Handling Unread Message Count in Conversations
IM clients often need to count unread messages in conversations.
You can use the interfaces provided by the IMLib SDK to directly retrieve the unread message count in conversations. The specific capabilities are as follows:
- Get the total number of unread messages in all conversations (excluding chatrooms) (
getTotalUnreadCount
) - Get the total number of unread messages in a specified conversation, or the total number of unread messages of a specified message type in a specified conversation, or the total number of unread messages by conversation type (
getUnreadCount
)
When users are using your app, the unread count on the UI may need to change. In this case, you can clear the unread count in the conversation (clearMessagesUnreadStatus
).
Get the Total Unread Message Count for All Conversations
You can use getTotalUnreadCount
to get the total number of unread messages in all types of conversations (excluding chatrooms).
RongIMClient.getInstance().getTotalUnreadCount(callback);
Upon successful retrieval, the callback
will return the unread message count (unReadCount
).
RongIMClient.getInstance().getTotalUnreadCount(new ResultCallback<Integer>() {
@Override
public void onSuccess(Integer unReadCount) {
}
@Override
public void onError(RongIMClient.ErrorCode ErrorCode) {
}
});
Get the Total Unread Message Count for a Specified Conversation
Get the total number of unread messages in a specified conversation.
RongIMClient.getInstance().getUnreadCount(conversationType, targetId, callback);
Parameter | Type | Description |
---|---|---|
conversationType | ConversationType | Conversation type. Not applicable to chatrooms or ultra groups. |
targetId | String | Conversation ID |
callback | ResultCallback<Integer> | Callback interface |
Upon successful retrieval, the callback
will return the unread message count (unReadCount
).
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation Id";
RongIMClient.getInstance().getUnreadCount(conversationType, targetId,
new ResultCallback<Integer>() {
@Override
public void onSuccess(Integer unReadCount) {
}
@Override
public void onError(RongIMClient.ErrorCode ErrorCode) {
}
});
Get the Total Unread Message Count for Specified Message Types in a Specified Conversation
This method was introduced in SDK version 5.1.5. This method is only available in RongCoreClient.
Get the unread count for one or more specified message types in a specified conversation.
RongCoreClient.getInstance().getUnreadCount(targetId, conversationType, objectNames, callback)
Parameter | Type | Description |
---|---|---|
targetId | String | Conversation ID. |
conversationType | ConversationType | Conversation type. Not applicable to chatrooms or ultra groups. |
objectNames | String[] | Array of message type identifiers. For built-in message type identifiers, see Message Type Overview. |
callback | IRongCoreCallback.ResultCallback | Callback result. |
Upon successful retrieval, the callback
will return the unread message count (unReadCount
).
Get the Total Unread Message Count by Conversation Type
Get the unread count for multiple specified conversation types.
RongIMClient.getInstance().getUnreadCount(conversationTypes, containBlocked, callback);
Parameter | Type | Description |
---|---|---|
conversationTypes | ConversationType [] | Array of conversation types. Not applicable to chatrooms or ultra groups. |
containBlocked | boolean | Whether to include unread messages from muted conversations. true : include. false : exclude. |
callback | ResultCallback<Integer> | Callback interface |
Upon successful retrieval, the callback
will return the unread message count (unReadCount
).
ConversationTypes[] conversationTypes = {ConversationTypes.PRIVATE, ConversationTypes.GROUP};
boolean containBlocked = true;
RongIMClient.getInstance().getUnreadCount(conversationTypes, containBlocked,
new ResultCallback<Integer>() {
@Override
public void onSuccess(Integer unReadCount) {
}
@Override
public void onError(RongIMClient.ErrorCode ErrorCode) {
}
});
Get the Total Unread Message Count by Conversation Do Not Disturb Level
This interface is supported starting from SDK version 5.2.5. It is only available in ChannelClient.
Get the total unread message count for conversations that have been set to a specified Do Not Disturb level. The SDK will search for conversations based on the Do Not Disturb level configuration passed in and return the total unread message count for all these conversations.
ChannelClient.getInstance().getUnreadCount(conversationTypes, levels, callback)
Parameter | Type | Required |
---|---|---|
conversationTypes | ConversationType [] | Array of conversation types. Not applicable to chatrooms. |
levels | [PushNotificationLevel] [] | Array of Do Not Disturb types. For details, see Do Not Disturb Feature Overview. |
callback | ResultCallback<Integer> | Callback interface |
Upon successful retrieval, the callback
will return the unread message count (unReadCount
).
ConversationTypes[] conversationTypes = {ConversationTypes.PRIVATE, ConversationTypes.GROUP};
PushNotificationLevel[] levels = {PushNotificationLevel.PUSH_NOTIFICATION_LEVEL_MENTION}
ChannelClient.getInstance().getUnreadCount(conversationTypes, levels,
new ResultCallback<Integer>() {
@Override
public void onSuccess(Integer unReadCount) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode ErrorCode) {
}
});
Get the Total Unread @ Message Count by Conversation Do Not Disturb Level
This interface is supported starting from SDK version 5.2.5. It is only available in ChannelClient.
Get the total unread @ message count for conversations that have been set to a specified Do Not Disturb level. The SDK will search for conversations based on the Do Not Disturb level configuration passed in and return the total unread @ message count for all these conversations.
ChannelClient.getInstance().getUnreadMentionedCount(conversationTypes, levels, callback)
Parameter | Type | Required |
---|---|---|
conversationTypes | ConversationType [] | Array of conversation types. Not applicable to chatrooms. |
levels | [PushNotificationLevel] [] | Array of Do Not Disturb types. For details, see Do Not Disturb Feature Overview. |
callback | ResultCallback<Integer> | Callback interface |
Upon successful retrieval, the callback
will return the unread @ message count (unReadCount
).
ConversationTypes[] conversationTypes = {ConversationTypes.PRIVATE, ConversationTypes.GROUP};
PushNotificationLevel[] levels = {PushNotificationLevel.PUSH_NOTIFICATION_LEVEL_MENTION}
ChannelClient.getInstance().getUnreadMentionedCount(conversationTypes, levels,
new ResultCallback<Integer>() {
@Override
public void onSuccess(Integer unReadCount) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode ErrorCode) {
}
});
Clear the Unread Count for a Specified Conversation
Clear the unread count for a specified conversation by timestamp. The SDK will clear the unread status of all messages before this timestamp.
RongIMClient.getInstance().clearMessagesUnreadStatus(conversationType, targetId, timestamp, callback);
Parameter | Type | Description |
---|---|---|
conversationType | ConversationType | Conversation type. Not applicable to chatrooms or ultra groups. |
targetId | String | Conversation ID |
timestamp | long | Timestamp. All unread messages before this timestamp will be cleared. |
callback | OperationCallback | Callback interface |
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation Id";
long timestamp = 1585811571;
RongIMClient.getInstance().clearMessagesUnreadStatus(conversationType, targetId, timestamp, new OperationCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});