Get Unread Message Count
The IMLib SDK allows you to retrieve unread message counts for ultra groups, including:
- Unread message counts for all ultra groups, specified ultra groups, or specified channels that the current user has joined.
- Unread @ message counts for all ultra groups, specified ultra groups, or specified channels that the current user has joined.
- Total unread message counts filtered by Do Not Disturb level.
- The maximum returned unread count is 999. If the actual count exceeds 999, the interface will still return 999.
The IMLib SDK only provides these interfaces in ChannelClient.
Get Unread Message Counts for Multiple Ultra Groups
The SDK supports retrieving unread message counts and unread @ message counts for all ultra groups the current user has joined.
Batch Retrieve Unread Message Counts for Current User's Ultra Groups
Supported since IMLib SDK v5.4.6.
In community scenarios, if you need to display real-time unread message data across multiple ultra group channels, use getUltraGroupConversationUnreadInfoList to fetch unread data for up to 20 ultra groups simultaneously. This includes:
- Unread message count per ultra group channel
- Unread @ message count per ultra group channel
- Unread @ messages specifically targeting the current user per channel
- Do Not Disturb level per ultra group channel
Interface
ChannelClient.getInstance().getUltraGroupConversationUnreadInfoList(targetIds,callback);
Parameters
| Parameter | Type | Description |
|---|---|---|
| targetIds | String[] | List of ultra group conversation targetIds (max 20). |
| callback | ResultCallback<List<ConversationUnreadInfo>> | Callback interface |
Sample Code
ChannelClient.getInstance().getUltraGroupConversationUnreadInfoList(targetIds,
new IRongCoreCallback.ResultCallback<List<ConversationUnreadInfo>>() {
@Override
public void onSuccess(List<ConversationUnreadInfo> ConversationUnreadInfos) {
if (ConversationUnreadInfos == null) return;
for (ConversationUnreadInfo unreadInfo : ConversationUnreadInfos) {
// Get ultra group conversation type
Conversation.ConversationType type = unreadInfo.getType();
String targetId = unreadInfo.getTargetId();
// Get ultra group channel ID
String channelId = unreadInfo.getChannelId();
// Get unread message count in channel
int unreadMessageCount = unreadInfo.getUnreadMessageCount();
// Get unread @ message count in channel
int unreadMentionedCount = unreadInfo.getUnreadMentionedCount();
// Get unread messages specifically mentioning current user
int mentionedMeCount = unreadInfo.getUnreadMentionedMeCount();
// Get channel's Do Not Disturb level
IRongCoreEnum.PushNotificationLevel pushNotificationLevel = getPushNotificationLevel();
}
// This callback returns on non-UI thread. Switch to UI thread for UI operations.
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode ErrorCode) {
}
});
Get Total Unread Count for Current User's Ultra Groups
Retrieves the sum of unread messages across all ultra group conversations the current user has joined.
Interface
class ResultCallback {
onSuccess(Integer count)
onError(ErrorCode code)
}
public void getUltraGroupAllUnreadCount(final IRongCoreCallback.ResultCallback<Integer> callback)
Get Total Unread @ Count for Current User's Ultra Groups
Retrieves the sum of unread @ messages across all ultra group conversations the current user has joined.
Interface
class ResultCallback {
onSuccess(Integer count)
onError(ErrorCode code)
}
public void getUltraGroupAllUnreadMentionedCount(IRongCoreCallback.ResultCallback<Integer> callback) {}
Get Unread Message Count for a Single Ultra Group
The SDK supports retrieving unread message counts and unread @ message counts for specified ultra groups or channels.
Get Unread Count for Specified Ultra Group
Retrieves the unread message count for the current user in a specified ultra group.
Interface
class ResultCallback {
onSuccess(Integer count)
onError(ErrorCode code)
}
public void getUltraGroupUnreadCount(String targetId, IRongCoreCallback.ResultCallback<Integer> callback) {}
Get Unread @ Count for Specified Ultra Group
Retrieves the unread @ message count for the current user in a specified ultra group.
Interface
class ResultCallback {
onSuccess(Integer count)
onError(ErrorCode code)
}
public void getUltraGroupUnreadMentionedCount(final String targetId, final IRongCoreCallback.ResultCallback<Integer> callback)
Get Unread Count for Specified Channel in Ultra Group
Retrieves the unread message count for the current user in a specified channel of an ultra group conversation.
Interface
class ResultCallback {
onSuccess(Integer count)
onError(ErrorCode code)
}
public void getUnreadCount(final Conversation.ConversationType conversationType,
final String targetId,
final String channelId,
final IRongCoreCallback.ResultCallback<Integer> callback)
Get Unread @ Count for Specified Channel
Apps can directly obtain unread @ counts from the Conversation object:
getUnreadMentionedCount(): Sum of unread "@all" and "@current user" messages in the channel.getUnreadMentionedMeCount(): Count of unread messages specifically "@current user". Requires SDK version ≥ 5.4.5.
For examples, see Get Channel List.
Get Total Unread Count for Ultra Groups Filtered by Do Not Disturb Level
Supported since SDK v5.2.5. Only available in ChannelClient.
Retrieves the total unread message count for conversations and channels matching specified Do Not Disturb levels. The SDK searches based on the provided level configuration and returns the total unread count for matching channels.
Interface
ChannelClient.getInstance().getUltraGroupUnreadCount(targetId, levels,callback);
Parameters
| Parameter | Type | Description |
|---|---|---|
| targetId | String | Conversation ID |
| levels | PushNotificationLevel[] | Array of Do Not Disturb types. See [Do Not Disturb Overview]. |
| callback | ResultCallback<Integer> | Callback interface |
Upon success, the callback returns the unread message count (unReadCount).
Sample Code
PushNotificationLevel[] levels = {PushNotificationLevel.PUSH_NOTIFICATION_LEVEL_MENTION}
ChannelClient.getInstance().getUltraGroupUnreadCount(targetId, levels,
new ResultCallback<Integer>() {
@Override
public void onSuccess(Integer unReadCount) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode ErrorCode) {
}
});
Get Total Unread @ Mentions Count in Ultra Groups by Do Not Disturb Level
This interface is supported starting from SDK version 5.2.5. It is only available in ChannelClient.
You can retrieve the total count of unread @ mentions for conversations and channels that have specific Do Not Disturb levels configured. The IMLib SDK will search based on the provided Do Not Disturb level settings and return the total count of unread @ mentions in channels that match the specified settings within the conversation.
Interface
ChannelClient.getInstance().getUltraGroupUnreadMentionedCount(targetId, levels,callback);
#### Parameter Description
| Parameter | Type | Description |
| :--- | :--- | :---: |
| targetId | String | Conversation ID |
| levels | `PushNotificationLevel[]` | Array of Do Not Disturb types. See [Do Not Disturb Overview]. |
| callback | ResultCallback\<Integer\> | Callback interface |
Upon successful retrieval, the `callback` will return the unread message count (`unReadCount`).
#### Sample Code
```java
PushNotificationLevel[] levels = {PushNotificationLevel.PUSH_NOTIFICATION_LEVEL_MENTION}
ChannelClient.getInstance().getUltraGroupUnreadMentionedCount(targetId, levels,
new ResultCallback<Integer>() {
@Override
public void onSuccess(Integer unReadCount) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode ErrorCode) {
}
});
If you need to retrieve the total unread message count for multiple conversation types, refer to the methods described in [Handling Conversation Unread Counts](../conversation/handle-unread-count.md).
<!-- links -->
[Do Not Disturb Overview]: ./do-not-disturb-about.md