Skip to main content

Insert Messages

Feature Description

You can use the insertOutgoingMessage or insertIncomingMessage method of IMCenter to insert a message into the chat UI. Messages inserted via this method will be added to the data source and update the UI accordingly.

This method inserts either a sent or received message into the local conversation. The message type must be one that the client stores. For details, refer to the Message Annotations section in the Message Introduction documentation.

Note: This method only stores the message in the SDK's local database and does not actually send it to the server or the recipient. The UI will refresh automatically after insertion.

Insert Outgoing Message

Interface

IMCenter.getInstance().insertOutgoingMessage(conversationType, targetId, sentStatus, content, sentTime, callback);

Parameters

ParameterTypeDescription
conversationTypeConversationTypeConversation type
targetIdStringConversation ID. For details about Target ID, see the Message Introduction.
sentStatusMessage.SentStatusMessage sending status
contentMessageContentMessage content
sentTimelongMessage timestamp.
The message list page sorts and displays messages based on this time.
callbackIRongCallback.ResultCallback<Message>Callback interface

Sample Code

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "user1";
SentStatus sentStatus = SentStatus.SENT;
TextMessage content = TextMessage.obtain("This is the message content");
String sentTime = System.currentTimeMillis();

IMCenter.getInstance().insertOutgoingMessage(conversationType, targetId, sentStatus, content, sentTime, new RongIMClient.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 Incoming Message

Interface

IMCenter.getInstance().insertIncomingMessage(conversationType, targetId, senderUserId, receivedStatus, content, sentTime, callback);

Parameters

ParameterTypeRequiredDescription
conversationTypeConversationTypeYesConversation type
targetIdStringYesTarget ID identifies the conversation. Note: For one-to-one chats, the Target ID is always the peer user ID. For group chats and ultra groups, the Target ID is the group ID or ultra group ID, respectively. For details, see the Message Introduction.
senderUserIdStringYesSender ID
receivedStatusMessage.ReceivedStatusYesReceipt status
contentMessageContentYesMessage content
sentTimelongYesMessage timestamp.
The message list page sorts and displays messages based on this time.
callbackIRongCallback.ResultCallback<Message>YesCallback interface

Sample Code

ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "user1";
String senderUserId = "Mock sender ID";
ReceivedStatus receivedStatus = new ReceivedStatus(0x1);
TextMessage content = TextMessage.obtain("This is an inserted message");
String sentTime = System.currentTimeMillis();

IMCenter.getInstance().insertIncomingMessage(conversationType, targetId, senderUserId, receivedStatus, content, sentTime, new RongIMClient.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) {

}
});