Skip to main content

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 sentTime carefully — 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
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
}

Insert an incoming message

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
}

Insert multiple messages

Max 500 messages per call. The following NCInsertMessageParams properties are persisted:

PropertyTypeDescription
contentNCMessageContent *Message content (required)
directionNCMessageDirectionSend or receive. Default: .send.
senderUserIdNSString *Sender user ID. nil uses SDK default.
sentStatusNCSentStatusSending status (for outgoing messages). Default: .sent.
receivedStatusInfoNCMessageReceivedStatusInfo *Receive status (for incoming messages).
sentTimeint64_tSent time in milliseconds. 0 uses current local time.
metadataNSDictionary *Message extension metadata.