Insert messages
The Nexconn SDK supports inserting messages into the local database. Inserted messages are not sent to the server or to other users.
tip
- Inserted messages exist only in the local database.
- They are not synced when the user reinstalls the app or logs in on another device.
Insert messages
The SDK provides a batch insert method that supports both sent and received messages.
- Kotlin
- Java
kotlin
// Get the target channel instance first
val directChannel = DirectChannel("userId")
val groupChannel = GroupChannel("groupId")
// Insert a sent message into a direct channel
directChannel.insertMessages(
listOf(
InsertMessageItem(
content = TextMessage("Message content 1"),
direction = MessageDirection.SEND,
sentTime = System.currentTimeMillis(),
)
)
) { success, error ->
if (error == null && success == true) {
Log.d("Insert", "Messages inserted")
}
}
// Insert a received message into a group channel
groupChannel.insertMessages(
listOf(
InsertMessageItem(
content = TextMessage("Message content 2"),
direction = MessageDirection.RECEIVE,
senderUserId = "senderUserId",
sentTime = System.currentTimeMillis(),
)
)
) { success, error ->
if (error == null && success == true) {
Log.d("Insert", "Message inserted")
}
}
Java
// Get the target channel instance first
DirectChannel directChannel = new DirectChannel("userId");
GroupChannel groupChannel = new GroupChannel("groupId");
// Insert a sent message into a direct channel
List<InsertMessageItem> items1 = Collections.singletonList(
new InsertMessageItem(
new TextMessage("Message content 1"),
MessageDirection.SEND,
"",
System.currentTimeMillis(),
SentStatus.SENT
)
);
directChannel.insertMessages(items1, (success, error) -> {
if (error == null && success) {
Log.d("Insert", "Messages inserted");
}
});
// Insert a received message into a group channel
List<InsertMessageItem> items2 = Collections.singletonList(
new InsertMessageItem(
new TextMessage("Message content 2"),
MessageDirection.RECEIVE,
"senderUserId",
System.currentTimeMillis(),
SentStatus.SENT
)
);
groupChannel.insertMessages(items2, (success, error) -> {
if (error == null && success) {
Log.d("Insert", "Message inserted");
}
});
Parameters
channel.insertMessages(items, handler)
| Parameter | Type | Description |
|---|---|---|
items | List<InsertMessageItem> | The message items to insert. |
handler | OperationHandler<Boolean>? | The completion callback. result is true on success. |
InsertMessageItem properties
| Property | Type | Description |
|---|---|---|
content | MessageContent | The message content. |
direction | MessageDirection | The message direction (SEND or RECEIVE). Default: SEND. |
senderUserId | String | The sender user ID. Default: "". Required for received messages. |
sentTime | Long | The sent timestamp in milliseconds. Default: current time. |
sentStatus | SentStatus | Sending status. Default: SentStatus.SENT. |
Important notes
- Insert no more than 500 messages per call. 10 or fewer is recommended.
sentTimeaffects message ordering. Ensure timestamps are correct.