Insert Messages
The IMLib SDK supports inserting messages into the local database. Messages inserted locally will not actually be sent to the RC server or the recipient.
- All inserted messages must have their MessageTag set to
MessageTag.ISPERSISTED, otherwise a parameter error exception will be thrown. See Understanding Message Storage Strategies for details. - The message insertion interface only inserts messages into the local database, so when the app is uninstalled/reinstalled or logged in from a different device, the inserted messages will not be synced from the remote server to the local database.
Insert Single Outgoing Message
You can use the insertOutgoingMessage: interface to insert an outgoing message into the local database. The following example uses the insertOutgoingMessage method under ChannelClient.
- Incorrect sentTime may affect message ordering - use with caution!
- This interface does not support chatroom conversation types.
Interface
ChannelClient.getInstance().insertOutgoingMessage(conversationType, targetId, "", true, sentStatus, content, sentTime, callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| type | ConversationType | Conversation type. |
| targetId | String | Conversation ID. |
| channelId | String | Ultra group channel ID. Pass an empty string for one-to-one or group chat conversation types. |
| canIncludeExpansion | boolean | Whether message expansion is supported. true means expandable; false means non-expandable. |
| sentStatus | Message.SentStatus | Sending status. |
| content | MessageContent | Message content. |
| sentTime | long | Message sending time as Unix timestamp (milliseconds). Passing 0 will use local time for insertion. |
| callback | IRongCoreCallback.ResultCallback<Message> | Callback interface. |
Example Code
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation Id";
SentStatus sentStatus = SentStatus.SENT;
TextMessage content = TextMessage.obtain("Here is the message content");
long sentTime = System.currentTimeMillis();
ChannelClient.getInstance().insertOutgoingMessage(conversationType, targetId, "", true, sentStatus, content, sentTime, new IRongCoreCallback.ResultCallback<Message>() {
/**
* Success callback
* @param message The inserted message
*/
@Override
public void onSuccess(Message message) {
}
/**
* Failure callback
* @param errorCode Error code
*/
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
}
});
For messages inserted into the local database to support message expansion functionality, you must use the insertion method under ChannelClient. Otherwise, the message's expandable property (canIncludeExpansion) cannot be enabled. Message expansion data cannot be set during local message insertion. The app can choose an appropriate time to set expansion data for the message. See [Message Expansion] for details.
Insert Single Incoming Message
You can use the insertIncomingMessage: interface to insert a received message into the local database.
Interface
RongCoreClient.getInstance().insertIncomingMessage(conversationType, targetId, senderUserId, receivedStatus, content, sentTime, callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| conversationType | ConversationType | Conversation type. |
| targetId | String | Target ID used to identify the conversation, also called target ID or conversation ID. Note: In one-to-one chats, the peer user ID is always used as the Target ID to identify the local conversation. Therefore, when inserting locally received messages in one-to-one conversations, the Target ID is always the peer user ID. For group chats and ultra groups, the conversation IDs are the group ID and ultra group ID respectively. See the explanation about Target ID in Message Introduction. |
| senderUserId | String | Sender's user ID. |
| receivedStatus | Message.ReceivedStatus | Receipt status. |
| content | MessageContent | Message content. |
| sentTime | long | Message sending time as Unix timestamp (milliseconds). Passing 0 will use local time for insertion. |
| callback | IRongCoreCallback.ResultCallback<Message> | Callback interface. |
Example Code
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation Id";
String senderUserId = "Simulated sender ID";
ReceivedStatus receivedStatus = new ReceivedStatus(0x1);
TextMessage content = TextMessage.obtain("This is an inserted message");
long sentTime = System.currentTimeMillis();
RongCoreClient.getInstance().insertIncomingMessage(conversationType, targetId, senderUserId, receivedStatus, content, sentTime, new IRongCoreCallback.ResultCallback<Message>() {
/**
* Success callback
* @param message The inserted message
*/
@Override
public void onSuccess(Message message) {
}
/**
* Failure callback
* @param errorCode Error code
*/
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
Insert Single Message
Insert a message into a local conversation. The message will not actually be sent to the server or the recipient. Chatroom conversation types are not supported.
Interface
ChannelClient.getInstance().insertMessage(message, callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| message | [Message] | The message. |
| callback | IRongCoreCallback.ResultCallback<Message> | Callback interface. |
Example Code
Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
String targetId = "Conversation Id";
String senderUserId = "Simulated sender ID";
Message.SentStatus sentStatus = Message.SentStatus.SENT;
Message.ReceivedStatus receivedStatus = new Message.ReceivedStatus(0x1);
TextMessage content = TextMessage.obtain("This is an inserted message");
Message.MessageDirection direction = Message.MessageDirection.SEND;
long sentTime = System.currentTimeMillis();
Message message = Message.obtain(targetId, conversationType, content);
message.setSentTime(sentTime);
message.setReceivedStatus(receivedStatus);
message.setSenderUserId(senderUserId);
message.setSentStatus(sentStatus);
message.setMessageDirection(direction);
ChannelClient.getInstance().insertMessage(message, new IRongCoreCallback.ResultCallback<Message>() {
/**
* Success callback
* @param message The inserted message
*/
@Override
public void onSuccess(Message message) {
}
/**
* Failure callback
* @param e Error code
*/
@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
}
});
Batch Insert Messages
- Supported since IMLib SDK version 5.1.1.
- Since batch insertion to the local database will fail entirely if any part fails, it's recommended to insert in batches. The maximum number of messages per operation is 500, with 10 messages per batch recommended.
- This feature is not supported for chatroom conversation types or ultra group conversation types.
The following attributes of Message will be stored, while others will be discarded:
-
UId: The globally unique message ID. After successful message transmission/reception, this ID is generated by the server. SDK supports storing this attribute starting from version 5.3.5, typically used for data migration. -
conversationType: Conversation type. -
targetId: Conversation ID. -
messageDirection: Message direction. -
senderUserId: Sender ID. -
receivedStatus: Receipt status; if the message direction is incoming andmessage.getReceivedStatus().setRead()hasn't been called, the message is marked as unread. Unread messages will increment the conversation's unread count. -
sentStatus: Sending status. -
content: Message content. -
sentTime: Unix timestamp (in milliseconds) when the message was sent, affecting message sorting. -
extra: Additional message information.
Interface
RongCoreClient.getInstance().batchInsertMessage(messages, callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| messages | List<Message> | Array of messages to insert. Maximum 500 messages per operation (10 recommended). Note: Each message must include a valid sentTime (Unix timestamp in milliseconds), otherwise the message cannot be retrieved from the database via the history message API. |
| callback | IRongCoreCallback.ResultCallback<Boolean> | Callback interface |
Sample Code
RongCoreClient.getInstance().batchInsertMessage(messages, new IRongCoreCallback.ResultCallback<Message>() {
/**
* Success callback
*/
@Override
public void onSuccess(Boolean success) {
}
/**
* Failure callback
* @param errorCode Error code
*/
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
An overload method is provided for batch message insertion. When enableCheck is set to true, the SDK will check for duplicate UIDs against the database and remove duplicates. Note: The app should ensure there are no duplicate UIDs within the message array before insertion.
public abstract void batchInsertMessage(
final List<Message> messages,
boolean enableCheck,
final IRongCoreCallback.ResultCallback<Boolean> callback);