Receiving Messages
Developers can intercept messages received by the SDK and perform corresponding business operations.
Listening for Message Reception
Applications can set multiple message reception listeners via the addOnReceiveMessageListener method. All received messages will be callbacked in the method of the OnReceiveMessageWrapperListener listener. It is recommended to register the message listener within the application lifecycle.
RongCoreClient.addOnReceiveMessageListener(
new io.rong.imlib.listener.OnReceiveMessageWrapperListener() {
@Override
public boolean onReceivedMessage(Message message, ReceivedProfile profile) {
int left = profile.getLeft();
boolean isOffline = profile.isOffline();
boolean hasPackage = profile.hasPackage();
}
});
The message reception listener OnReceiveMessageWrapperListener can receive real-time messages or offline messages.
When the client successfully connects, the server will send all offline messages? to the client in the form of message packages (Package), with each Package containing up to 200 messages. The client will parse the messages in the Package and notify the application one by one.
The SDK triggers the following method when a message is received.
public abstract void onReceivedMessage(Message message, ReceivedProfile profile);
ReceivedProfile
encapsulates the data related to the currently received message:
- The
left
inReceivedProfile
indicates the number of messages remaining in the currently parsed message package (Package). - The
hasPackage
inReceivedProfile
indicates whether there are still undelivered message packages (Package) on the server. - The
offline
inReceivedProfile
indicates whether the current message is an offline message.
The following conditions indicate that the offline messages have been fully received:
hasPackage
isfalse
: indicates that the last message package is currently being parsed.left
is 0: indicates that the last message in the last message package has been received.
Starting from version 5.2.3, the following callback method is triggered when the offline messages are fully received after each successful connection. If there are no offline messages, it will be triggered immediately after the connection is successful.
public void onOfflineMessageSyncCompleted() {
//
}
To avoid memory leaks, please call removeOnReceiveMessageListener to remove the listener when it is no longer needed.
RongCoreClient.removeOnReceiveMessageListener(listener);
Message Reception Status
The Message class encapsulates ReceivedStatus, which uses the following statuses to indicate the status of the received message.
Status | Description |
---|---|
isRead() | Whether it has been read. If the message is read on the current device, this status will change to read. Starting from SDK version 5.6.8, as long as the message is read on other devices, this status value on the current device will also change to read. |
isListened() | Whether it has been listened to, only for voice messages. |
isDownload() | Whether it has been downloaded, only for media messages. |
isRetrieved() | Whether the message has been received by other devices that are online or previously logged in. As long as other devices receive the message first, this status value will change to received. |
Disabling Message Deduplication Mechanism
The message deduplication mechanism automatically removes duplicate messages when the SDK receives one-to-one chat, group chat, system messages, and chatroom messages. When there are a large number of messages locally in the App, the default deduplication mechanism of the SDK may cause message reception lag due to performance issues. Therefore, when message reception lag occurs, you can try turning off the SDK's deduplication mechanism.
Why Message Duplication May Occur in Message Reception
This issue may occur when the sender is in a weak network condition. After A sends a message to B, the message successfully reaches the server and is successfully delivered to the receiver B. However, A may not receive the ack returned by the server due to network issues, causing A to believe that the message was not sent successfully. If A resends the message, B will receive a duplicate message (the message content is the same, but the Message UID is different).
Turning Off Message Deduplication
Starting from SDK version 5.3.4, message deduplication can be turned off. This interface is only provided in RongCoreClient. It is not supported to turn off message deduplication for chatroom and ultra group conversation types.
Please call this after SDK initialization and before establishing the IM connection. Multiple calls will take the last call as the standard.
boolean enableCheck = false // Turn off message deduplication
RongCoreClient.getInstance().setCheckDuplicateMessage(enableCheck)
Starting from version 5.8.2, chatroom messages support turning off message deduplication. It is provided in
RongChatRoomClient
.
Please call this after SDK initialization and before establishing the IM connection. Multiple calls will take the last call as the standard.
boolean enableCheck = false // Turn off message deduplication
RongChatRoomClient.getInstance().setCheckChatRoomDuplicateMessage(enableCheck)