Insert messages
Insert messages into the local database without sending them to the server or recipients.
tip
- Only insert message types that support local storage.
- Inserted messages are local-only and will not sync to other devices.
Insert messages
Use insertMessages:completion: on a channel instance to insert multiple local messages in one call. The maximum is 500 messages per call:
tip
- Set
sentTimecarefully — incorrect values affect message ordering. - Open channels are not supported.
Insert an outgoing message
Build an NCInsertMessageParams and call insertMessages on a channel instance:
- Swift
- Objective-C
swift
import NexconnChatSDK
let channel = DirectChannel(channelId: "targetUserId")
let content = TextMessage(text: "Test message")
let params = NCInsertMessageParams(content: content)
params.direction = MessageDirectionSend
params.sentStatus = NCSentStatusSent
params.sentTime = (int64_t)([[NSDate date] timeIntervalSince1970] * 1000)
params.senderUserId = "ownUserId"
channel insertMessages:@[params
completion:^(BOOL isInserted, NCError *error) {
// isInserted == YES 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 = NCSentStatusSent;
params.sentTime = (int64_t)([[NSDate date] timeIntervalSince1970] * 1000);
params.senderUserId = @"ownUserId";
[channel insertMessages:@[params]
completion:^(BOOL isInserted, NCError *error) {
// isInserted == YES on success
}];
Insert an incoming message
- Swift
- Objective-C
swift
import NexconnChatSDK
let channel = DirectChannel(channelId: "targetUserId")
let content = TextMessage(text: "Received text")
let params = NCInsertMessageParams(content: content)
params.direction = MessageDirectionReceive
params.senderUserId = "senderUserId"
params.sentTime = (int64_t)([[NSDate date] timeIntervalSince1970] * 1000)
channel insertMessages:@[params
completion:^(BOOL isInserted, NCError *error) {
// isInserted == YES 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 insertMessages:@[params]
completion:^(BOOL isInserted, NCError *error) {
// isInserted == YES on success
}];
Insert multiple messages
Max 500 messages per call. The following NCInsertMessageParams properties are persisted:
| Property | Type | Description |
|---|---|---|
content | NCMessageContent * | Message content (required) |
direction | NCMessageDirection | Send or receive. Default: .send. |
senderUserId | NSString * | Sender user ID. nil uses SDK default. |
sentStatus | NCSentStatus | Sending status (for outgoing messages). Default: .sent. |
receivedStatusInfo | NCMessageReceivedStatusInfo * | Receive status (for incoming messages). |
sentTime | int64_t | Sent time in milliseconds. 0 uses current local time. |
metadata | NSDictionary * | Message extension metadata. |