Skip to main content

Insert messages

Use NCChatUI.insertMessages(ChannelIdentifier, List<InsertMessageItem>, OperationHandler<Boolean>) to insert messages into a local channel.

Important
  • Inserted messages are written to the local database only and are not sent to the server.
  • These locally inserted messages will not be restored from the cloud after reinstalling the app or logging in from a different device.
  • After successful insertion, Chat UI dispatches an insert event and refreshes the corresponding page.

Interface

kotlin
NCChatUI.insertMessages(
identifier,
items
) { result, error ->
// error == null and result == true indicates success
}

Parameters

ParameterTypeRequiredDescription
identifierChannelIdentifierYesTarget channel identifier
itemsList<InsertMessageItem>YesCollection of messages to insert
handlerOperationHandler<Boolean>NoCallback; result=true indicates success

InsertMessageItem key fields:

FieldTypeDescription
contentMessageContentMessage content
directionMessageDirectionDirection (SEND / RECEIVE)
senderUserIdString?Sender ID (recommended for received message scenarios)
sentTimeLongSent timestamp (milliseconds)
sentStatusSentStatusSent status

Example

kotlin
val identifier = ChannelIdentifier(ChannelType.DIRECT, "user_001")

val outgoing = InsertMessageItem(
content = TextMessage("This is an inserted sent message"),
direction = MessageDirection.SEND,
senderUserId = NCEngine.getCurrentUserId(),
sentTime = System.currentTimeMillis(),
sentStatus = SentStatus.SENT
)

val incoming = InsertMessageItem(
content = TextMessage("This is an inserted received message"),
direction = MessageDirection.RECEIVE,
senderUserId = "user_001",
sentTime = System.currentTimeMillis(),
sentStatus = SentStatus.SENT
)

NCChatUI.insertMessages(identifier, listOf(outgoing, incoming)) { result, error ->
if (error == null && result == true) {
// Insertion successful
}
}