Skip to main content

Insert messages

The message insertion API does not send messages to the remote server, so inserted messages won't be synced to the local database when reinstalling the app or logging in from another device.

Insert outgoing messages

Use the insertMessagesWithParams:completion: interface to insert an outgoing message into the local database. The following example shows how to use NCDirectChannel's insertMessagesWithParams:completion: method.

tip
  • Incorrect sentTime values may affect message ordering - use with caution.
  • Successfully inserted messages won't immediately appear in the channel view.

Interface prototype

Objective C
- (void)insertMessagesWithParams:(NSArray<NCInsertMessageParams *> *)params
completion:(nullable void(^)(BOOL isInserted, NCError *_Nullable error))completion;

Parameters

ParameterTypeDescription
paramsNSArray<[NCInsertMessageParams]>List of message parameters to insert
completionBlockAsync callback returning insertion status

Example code

Objective C
NCTextMessage *content = [[NCTextMessage alloc] initWithText:@"Test text message"];
NCInsertMessageParams *params = [[NCInsertMessageParams alloc] initWithContent:content];
params.direction = NCMessageDirectionSend;
params.sentStatus = NCMessageSentStatusSent;
params.sentTime = sentTime;

NCDirectChannel *channel = [[NCDirectChannel alloc] initWithChannelId:@"targetId"];
[channel insertMessagesWithParams:@[ params ]
completion:^(BOOL isInserted, NCError * _Nullable error) {

}];

Insert incoming messages

Use the insertMessagesWithParams:completion: interface to insert an incoming message into the local database.

tip
  • Incorrect sentTime values may affect message ordering - use with caution.
  • Successfully inserted messages won't immediately appear in the channel view.

Interface prototype

Objective C
- (void)insertMessagesWithParams:(NSArray<NCInsertMessageParams *> *)params
completion:(nullable void(^)(BOOL isInserted, NCError *_Nullable error))completion;

Parameters

ParameterTypeDescription
paramsNSArray<[NCInsertMessageParams]>List of message parameters to insert
completionBlockAsync callback returning insertion status

Example code

Objective C
NCTextMessage *content = [[NCTextMessage alloc] initWithText:@"Test text message"];
NCMessageReceivedStatusInfo *info = [[NCMessageReceivedStatusInfo alloc] init];
info.isRead = NO;

NCInsertMessageParams *params = [[NCInsertMessageParams alloc] initWithContent:content];
params.direction = NCMessageDirectionReceive;
params.senderUserId = @"senderUserId";
params.receivedStatusInfo = info;
params.sentTime = 0;

NCDirectChannel *channel = [[NCDirectChannel alloc] initWithChannelId:@"targetId"];
[channel insertMessagesWithParams:@[ params ]
completion:^(BOOL isInserted, NCError * _Nullable error) {

}];

Display inserted messages in channel view

To immediately display inserted messages in the NCChannelViewController, call the appendAndDisplayMessage: method with the NCMessage object to update the UI.

Objective C
NCMessage *insertMessage;
[self appendAndDisplayMessage:insertMessage];