Insert messages
Insert messages into the local database without sending them to the server or recipients.
Use insertMessages(params:completion:) in Swift or insertMessagesWithParams:completion: in Objective-C on a channel instance to insert one or more local messages in one call. Pass multiple InsertMessageParams / NCInsertMessageParams objects in the same array to insert multiple messages. The maximum is 500 messages per call.
tip
- Only insert message types that support local storage.
- Inserted messages are local-only and will not sync to other devices.
- Set
sentTimecarefully — incorrect values affect message ordering.
Insert an outgoing message
Build insert message parameters and call insertMessages on a channel instance:
- Swift
- Objective-C
swift
import NexconnChatSDK
guard let channel = DirectChannel(channelId: "targetUserId") else {
return
}
let content = TextMessage(text: "Test message")
let params = InsertMessageParams(content: content)
params.direction = .send
params.sentStatus = .sent
params.sentTime = Int64(Date().timeIntervalSince1970 * 1000)
params.senderUserId = "ownUserId"
channel.insertMessages(params: [params]) { isInserted, error in
// isInserted == true on success
}
Objective C
NCDirectChannel *channel = [[NCDirectChannel alloc] initWithChannelId:@"targetUserId"];
NCTextMessage *content = [[NCTextMessage alloc] initWithText:@"Test message"];
NCInsertMessageParams *params = [[NCInsertMessageParams alloc] initWithContent:content];
params.direction = NCMessageDirectionSend;
params.sentStatus = NCMessageSentStatusSent;
params.sentTime = (int64_t)([[NSDate date] timeIntervalSince1970] * 1000);
params.senderUserId = @"ownUserId";
[channel insertMessagesWithParams:@[params]
completion:^(BOOL isInserted, NCError *error) {
// isInserted == YES on success
}];
Insert an incoming message
- Swift
- Objective-C
swift
import NexconnChatSDK
guard let channel = DirectChannel(channelId: "targetUserId") else {
return
}
let content = TextMessage(text: "Received text")
let params = InsertMessageParams(content: content)
params.direction = .receive
params.senderUserId = "senderUserId"
params.sentTime = Int64(Date().timeIntervalSince1970 * 1000)
channel.insertMessages(params: [params]) { isInserted, error in
// isInserted == true on success
}
Objective C
NCDirectChannel *channel = [[NCDirectChannel alloc] initWithChannelId:@"targetUserId"];
NCTextMessage *content = [[NCTextMessage alloc] initWithText:@"Received text"];
NCInsertMessageParams *params = [[NCInsertMessageParams alloc] initWithContent:content];
params.direction = NCMessageDirectionReceive;
params.senderUserId = @"senderUserId";
params.sentTime = (int64_t)([[NSDate date] timeIntervalSince1970] * 1000);
[channel insertMessagesWithParams:@[params]
completion:^(BOOL isInserted, NCError *error) {
// isInserted == YES on success
}];
Insert message parameters
Use InsertMessageParams in Swift or NCInsertMessageParams in Objective-C to describe each local message to insert. The following properties are persisted:
- Swift
- Objective-C
| Property | Type | Description |
|---|---|---|
content | MessageContent | Message content (required) |
direction | MessageDirection | Send or receive. Default: .send. |
senderUserId | String | Sender user ID. nil uses SDK default. |
sentStatus | SentStatus | Sending status (for outgoing messages). Default: .sent. |
receivedStatusInfo | MessageReceivedStatusInfo | Receive status (for incoming messages). |
sentTime | Int64 | Sent time in milliseconds. 0 uses current local time. |
| Property | Type | Description |
|---|---|---|
content | NCMessageContent | Message content (required) |
direction | NCMessageDirection | Send or receive. Default: NCMessageDirectionSend. |
senderUserId | NSString | Sender user ID. nil uses SDK default. |
sentStatus | NCMessageSentStatus | Sending status (for outgoing messages). Default: NCMessageSentStatusSent. |
receivedStatusInfo | NCMessageReceivedStatusInfo | Receive status (for incoming messages). |
sentTime | int64_t | Sent time in milliseconds. 0 uses current local time. |