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
- Java
kotlin
NCChatUI.insertMessages(
identifier,
items
) { result, error ->
// error == null and result == true indicates success
}
Java
NCChatUI.insertMessages(identifier, items, (result, error) -> {
// error == null and result == true indicates success
});
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
identifier | ChannelIdentifier | Yes | Target channel identifier |
items | List<InsertMessageItem> | Yes | Collection of messages to insert |
handler | OperationHandler<Boolean> | No | Callback; result=true indicates success |
InsertMessageItem key fields:
| Field | Type | Description |
|---|---|---|
content | MessageContent | Message content |
direction | MessageDirection | Direction (SEND / RECEIVE) |
senderUserId | String? | Sender ID (recommended for received message scenarios) |
sentTime | Long | Sent timestamp (milliseconds) |
sentStatus | SentStatus | Sent status |
Example
- Kotlin
- Java
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
}
}
Java
ChannelIdentifier identifier = new ChannelIdentifier(ChannelType.DIRECT, "user_001");
InsertMessageItem outgoing = new InsertMessageItem(
new TextMessage("This is an inserted sent message"),
MessageDirection.SEND,
NCEngine.getCurrentUserId(),
System.currentTimeMillis(),
SentStatus.SENT);
InsertMessageItem incoming = new InsertMessageItem(
new TextMessage("This is an inserted received message"),
MessageDirection.RECEIVE,
"user_001",
System.currentTimeMillis(),
SentStatus.SENT);
NCChatUI.insertMessages(identifier, Arrays.asList(outgoing, incoming), (result, error) -> {
if (error == null && Boolean.TRUE.equals(result)) {
// Insertion successful
}
});