Skip to main content

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 sentTime carefully — incorrect values affect message ordering.

Insert an outgoing message

Build insert message parameters and call insertMessages on a channel instance:

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
}

Insert an incoming message

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
}

Insert message parameters

Use InsertMessageParams in Swift or NCInsertMessageParams in Objective-C to describe each local message to insert. The following properties are persisted:

PropertyTypeDescription
contentMessageContentMessage content (required)
directionMessageDirectionSend or receive. Default: .send.
senderUserIdStringSender user ID. nil uses SDK default.
sentStatusSentStatusSending status (for outgoing messages). Default: .sent.
receivedStatusInfoMessageReceivedStatusInfoReceive status (for incoming messages).
sentTimeInt64Sent time in milliseconds. 0 uses current local time.