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
Parameter | Type | Description |
---|---|---|
conversationType | ConversationType | Conversation type |
targetId | String | Conversation ID. For details about Target ID, see the Message Introduction. |
sentStatus | Message.SentStatus | Message sending status |
content | MessageContent | Message content |
sentTime | long | Message timestamp. The message list page sorts and displays messages based on this time. |
callback | IRongCallback.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
Parameter | Type | Required | Description |
---|---|---|---|
conversationType | ConversationType | Yes | Conversation type |
targetId | String | Yes | Target 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. |
senderUserId | String | Yes | Sender ID |
receivedStatus | Message.ReceivedStatus | Yes | Receipt status |
content | MessageContent | Yes | Message content |
sentTime | long | Yes | Message timestamp. The message list page sorts and displays messages based on this time. |
callback | IRongCallback.ResultCallback<Message> | Yes | Callback 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) {
}
});