Receiving Messages
You can listen for messages received by the IMKit SDK and perform corresponding business operations.
Message Reception Listener Overview
The SDK provides a message reception listener OnReceiveMessageWrapperListener that can receive both real-time and offline messages.
When the client successfully connects, the server delivers all offline messages? to the client in packages, with each package containing up to 200 messages. The SDK parses these messages and notifies the app by delivering them one by one.
The following method is triggered when the SDK receives a message:
public boolean onReceived(Message message, int left, boolean hasPackage, boolean offline)
left
indicates the number of remaining messages in the current package being parsed.hasPackage
indicates whether there are undelivered message packages on the server.offline
indicates whether the current message is an offline message.
Offline message reception is considered complete when both of the following conditions are met:
hasPackage
isfalse
: Indicates the current package is the last one.left
is 0: Indicates the last message in the final package has been received.
Starting from version 5.2.3, the following callback method is triggered when offline messages are fully received after each successful connection. If no offline messages exist, this callback is triggered immediately upon connection.
Prior to version 5.6.9, the IMCenter class's onOfflineMessageSyncCompleted method had synchronization issues. We recommend using the newer SDK version.
public void onOfflineMessageSyncCompleted() {
//
}
Setting Up Message Reception Listeners
The IMKit SDK provides addOnReceiveMessageListener
/removeOnReceiveMessageListener
methods in the IMCenter class, supporting multiple message reception listeners.
Adding a Message Listener
All received messages are delivered via the listener callback. We recommend registering the listener during the app's lifecycle. Avoid duplicate registration to prevent multiple callbacks and potential memory leaks. To ensure all offline messages are received, set up the listener before connecting to the RC server.
Call the addOnReceiveMessageListener
method in the IMCenter class. The callback thread behavior varies by SDK version:
- For SDK version = 5.3.3,
onReceivedMessage
runs on a non-main thread. - For SDK versions ≤ 5.3.2 or ≥ 5.3.4,
onReceivedMessage
runs on the main thread.
IMCenter.getInstance().addOnReceiveMessageListener(
new RongIMClient.OnReceiveMessageWrapperListener() {
@Override
public boolean onReceived(Message message, int left, boolean hasPackage, boolean offline) {
return false;
}
});
Removing a Message Listener
The SDK supports listener removal. To prevent memory leaks, remove listeners when they're no longer needed.
IMCenter.getInstance().removeOnReceiveMessageListener(listener);
Setting Up Asynchronous Message Reception Listeners
Available since IMKit 5.3.4. By default, IMKit SDK uses addAsyncOnReceiveMessageListener
.
The IMKit SDK provides addAsyncOnReceiveMessageListener
/removeAsyncOnReceiveMessageListener
methods in the IMCenter class, supporting multiple asynchronous message reception listeners.
Adding an Asynchronous Message Listener
For SDK versions ≥ 5.3.4, call addAsyncOnReceiveMessageListener
in the IMCenter class. The onReceivedMessage
callback runs on a non-main thread. Register the listener during the app's lifecycle and avoid duplicate additions.
IMCenter.getInstance().addAsyncOnReceiveMessageListener(
new RongIMClient.OnReceiveMessageWrapperListener() {
@Override
public boolean onReceived(Message message, int left, boolean hasPackage, boolean offline) {
return false;
}
});
Removing an Asynchronous Message Listener
For SDK versions ≥ 5.3.4, call removeAsyncOnReceiveMessageListener
to remove the listener when it's no longer needed.
IMCenter.getInstance().removeAsyncOnReceiveMessageListener(listener);
Disabling Message Deduplication
The message deduplication mechanism automatically filters duplicate messages in one-to-one chats, group chats, and system messages. When an app has a large volume of local messages, the default deduplication may cause performance issues and message reception delays. If message reception becomes sluggish, consider disabling the SDK's deduplication mechanism.
Why Duplicate Messages May Occur
This typically happens when the sender has poor network connectivity. After UserA sends a message to UserB, the message reaches the server and is delivered to UserB. However, if UserA doesn't receive the server's acknowledgment due to network issues, UserA may resend the message (IMKit has automatic retry by default—we recommend disabling this). Consequently, UserB receives duplicate messages (same content but different Message UIDs).
Turning Off Message Deduplication
For one-to-one chats, group chats, and system messages, use the setCheckDuplicateMessage
method in the IMLib core class RongCoreClient (requires SDK version ≥ 5.3.4). Configure this after SDK initialization but before establishing an IM connection:
boolean enableCheck = false // Disable message deduplication
RongCoreClient.getInstance().setCheckDuplicateMessage(enableCheck)
Chatroom message deduplication can be disabled starting from version 5.8.2 via
RongChatRoomClient
.
Call this after SDK initialization but before establishing an IM connection. The last configuration takes effect.
boolean enableCheck = false // Disable message deduplication
RongChatRoomClient.getInstance().setCheckChatRoomDuplicateMessage(enableCheck)
Disabling Automatic Retry
The IMKit SDK enables automatic message retry by default. After disabling message deduplication, we recommend also disabling IMKit's retry mechanism to avoid receiving duplicate UID messages.
Configure this after SDK initialization but before establishing an IM connection:
RongConfigCenter.conversationConfig().rc_enable_resend_message = false;