Skip to main content

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 OnReceiveMessageWrapperListener's methods. It is recommended to register message listeners during 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 both real-time and offline messages.

When the client successfully connects, the server will deliver all offline messages? to the client in message packages (Packages), 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 following method is triggered when the SDK receives a message:

public abstract void onReceivedMessage(Message message, ReceivedProfile profile);

ReceivedProfile encapsulates data related to the currently received message:

  • The left field in ReceivedProfile indicates the number of remaining messages in the current message package (Package) being parsed.
  • The hasPackage field in ReceivedProfile indicates whether there are undelivered message packages (Packages) remaining on the server.
  • The offline field in ReceivedProfile indicates whether the current message is an offline message.

The following conditions must be met simultaneously to confirm that all offline messages have been received:

  • hasPackage is false: Indicates that the current message is the last package being parsed.
  • left is 0: Indicates that the last message in the final message package has been received.

Starting from version 5.2.3, the following callback method is triggered when offline message synchronization is completed after each successful connection. If there are no offline messages, this callback is triggered immediately after connection.

public void onOfflineMessageSyncCompleted() {
//
}

To prevent memory leaks, 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 states to indicate the status of received messages.

StatusDescription
isRead()Whether the message has been read. If the message is read on the current device, this status changes to "read." Starting from SDK version 5.6.8, if the message is read on any other device, this status on the current device will also change to "read."
isListened()Whether the message has been listened to (applies only to voice messages).
isDownload()Whether the message has been downloaded (applies only to media messages).
isRetrieved()Whether the message has been received by other devices that are simultaneously online or were previously logged in. If another device receives the message first, this status will change to "received."

Disabling Message Deduplication

The message deduplication mechanism automatically filters out duplicate messages when the SDK receives one-to-one chats, group chats, system messages, or chatroom messages. If the app has a large number of local messages, the SDK's default deduplication mechanism may cause message reception delays due to performance issues. If message reception becomes sluggish, you can try disabling the SDK's deduplication mechanism.

Why Duplicate Messages May Occur

This issue may arise when the sender has a weak network connection. For example, when User A sends a message to User B, the message successfully reaches the server and is delivered to User B. However, User A may not receive the acknowledgment from the server due to network issues, leading User A to assume the message was not sent successfully. If User A resends the message, User B will receive a duplicate message (with identical content but a different Message UID).

Disabling Message Deduplication

tip

Starting from SDK version 5.3.4, message deduplication can be disabled. This feature is only available in RongCoreClient. It is not supported for chatroom or ultra group conversation types.

Call this method after SDK initialization but before establishing an IM connection. The last call takes precedence.

boolean enableCheck = false // Disable message deduplication
RongCoreClient.getInstance().setCheckDuplicateMessage(enableCheck)

Starting from version 5.8.2, chatroom messages support disabling message deduplication. This feature is provided in RongChatRoomClient.

Call this method after SDK initialization but before establishing an IM connection. The last call takes precedence.

boolean enableCheck = false // Disable message deduplication
RongChatRoomClient.getInstance().setCheckChatRoomDuplicateMessage(enableCheck)