Skip to main content

Insert Messages

The SDK supports inserting messages into the local database. Messages inserted locally are not actually sent to the server or the other party.

tip

All inserted messages must have their MessageTag set to MessageTag.ISPERSISTED, otherwise a parameter error exception will be thrown.

Insert a Single Outgoing Message

Insert a message into a local conversation with the direction set to "send". The message is stored in the local database but is not sent to the server or the other party. This is not supported for chatroom conversation types. The following example uses the insertOutgoingMessage method under ChannelClient.

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation ID";
SentStatus sentStatus = SentStatus.SENT;
TextMessage content = TextMessage.obtain("This 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) {

}
});

If messages inserted into the local database need to support message expansion, the insertion method under ChannelClient must be used; otherwise, the message's expandable property (canIncludeExpansion) cannot be enabled. Locally inserted messages do not support setting message expansion data. The app can choose an appropriate time to set the expansion data for the message. For details, see Message Expansion.

ParameterTypeDescription
typeConversationTypeConversation type.
targetIdStringConversation ID.
channelIdStringUltra group channel ID. For one-to-one and group chats, pass an empty string.
canIncludeExpansionbooleanWhether message expansion is supported. true means expandable; false means not expandable.
sentStatusMessage.SentStatusSending status.
contentMessageContentMessage content.
sentTimelongThe message's sending time, Unix timestamp (milliseconds). Passing 0 will insert the message using the local time.
callbackIRongCoreCallback.ResultCallback<Message>Callback interface.

Insert a Single Incoming Message

Insert a message into a local conversation with the direction set to "receive". The message is not actually sent to the server or the other party. This is not supported for chatroom conversation types.

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation ID";
String senderUserId = "Simulated sender's 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) {

}
});
ParameterTypeDescription
conversationTypeConversationTypeConversation type.
targetIdStringTarget ID, used to identify the conversation, also known as the conversation ID. Note: In one-to-one chats, the Target ID is always the other party's user ID, so when inserting a locally received message in a one-to-one conversation, the Target ID is always the other party's user ID. For group chats and ultra groups, the conversation ID is the group ID and ultra group ID, respectively. For details, see the explanation of Target ID in Message Introduction.
senderUserIdStringThe sender's user ID.
receivedStatusMessage.ReceivedStatusReceiving status.
contentMessageContentMessage content.
sentTimelongThe message's sending time, Unix timestamp (milliseconds). Passing 0 will insert the message using the local time.
callbackIRongCoreCallback.ResultCallback<Message)>Callback interface.

Insert a Single Message

Insert a message into a local conversation. The message is not actually sent to the server or the other party. This is not supported for chatroom conversation types.

        Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
String targetId = "Conversation ID";
String senderUserId = "Simulated sender's 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) {

}
});
});
ParameterTypeDescription
message[Message]The message.
callbackIRongCoreCallback.ResultCallback<Message)>Callback interface.

Batch Insert Messages

Insert multiple messages into the local database in bulk. Since a failure in batch insertion will result in the entire operation failing, it is recommended to insert in batches. This interface does not support chatroom or ultra group conversation types.

The following attributes of the Message will be stored in the database; other attributes will be discarded:

  • UId: The globally unique ID of the message. After the message is successfully sent or received, it will have a globally unique ID generated by the server. The SDK supports storing this attribute starting from version 5.3.5. This field is generally used for data migration.
  • conversationType: Conversation type.
  • targetId: Conversation ID.
  • messageDirection: Message direction.
  • senderUserId: Sender ID.
  • receivedStatus: Receiving status; if the message direction is "receive" and message.getReceivedStatus().setRead() has not been called, the message is considered unread. The unread message count will be added to the conversation's unread message count.
  • sentStatus: Sending status.
  • content: Message content.
  • sentTime: The message's sending time, Unix timestamp in milliseconds, which affects message sorting.
  • extra: Additional information of the message.
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) {

}
});
ParameterTypeDescription
messagesList<Message>The array of messages to be inserted. A single operation can insert up to 500 messages, but it is recommended to insert 10 messages at a time. Note that the message must contain a correct and valid sentTime (the message's sending time, Unix timestamp in milliseconds); otherwise, the message cannot be retrieved from the database using the history message retrieval interface.
callbackIRongCoreCallback.ResultCallback<Boolean>Callback interface.

The batch insert message method provides the following overloaded method. Setting enableCheck to true will cause the SDK to check for duplicate UIDs in the database during insertion and remove duplicates. Note that the app should ensure that the array of messages to be inserted does not contain duplicate UIDs internally.

    public abstract void batchInsertMessage(
final List<Message> messages,
boolean enableCheck,
final IRongCoreCallback.ResultCallback<Boolean> callback);