Manage unread message counts
Introduction
Through ZIM, you can get the unread message count for an individual conversation, acquire the total unread message count for all conversations, and clear the aforementioned values.
Get the unread message count of one conversation
ZIM supports actively or passively obtaining the number of unread messages of a conversation.
Actively obtaining
To actively obtain the number of unread messages in a conversation, first call queryConversationList or queryConversation to obtain the target conversation object. Then, you can use the unreadMessageCount property of the target conversation object to know the number of unread messages in that conversation.
Passively obtaining
Listen for the onConversationChanged callback to obtain the unreadMessageCount property of a conversation and know the latest number of unread messages in that conversation. For more details, refer to Get conversation list - Listen for conversation changes.
Get the total unread message count of all conversations
To know how many unread messages you currently have after login, listen for the callback onConversationTotalUnreadMessageCountUpdated of thesetEventHandlermethod.
After a successful login, the client user is notified of the update of the total number of unread messages through the callback in any of the following situations:
- The user receives a new message and the message notification is enabled for the current conversation.
- The user proactively clears the number of unread conversation messages. For details, see the chapter above Clear the number of unread conversation messages.
With this callback, you can adjust your app's UI display to remind the client user how many messages are currently unread.
// Set up the event handler and listen for related callbacks.
zim.setEventHandler(this);
...
// Callback for getting the total number of unread messages.
public void onConversationTotalUnreadMessageCountUpdated(ZIM zim, int totalUnreadMessageCount) {
// Get the number of unread messages for UI dispaly.
// ......
}
Clear the unread message count of one conversation
To clear the unread message number of one conversation after getting the conversation list, call the clearConversationUnreadMessageCount method.
Because the ZIM SDK does not know when your app users should clear the unread count of the conversation, you need to call this method when your app users interact with specific pages. The following are some common scenarios to call this method:
- Users click a conversation to enter the chat interface.
- Users stay in the same conversation, and this method should be called every time the user receiving a new message.
- Users mark a specified conversation as read in the conversation list interface.
zim.clearConversationUnreadMessageCount("CONV_ID", ZIMConversationType.PEER, new ZIMConversationUnreadMessageCountClearedCallback() {
@Override
public void onConversationUnreadMessageCountCleared(ZIMError errorInfo) {
// Returned results of the clear operation.
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
}
});
Clear the total unread message count of all conversations
To clear the unread message numbers of all conversations after getting the conversation list, call the clearConversationTotalUnreadMessageCount method.
When you want to clear the unread message counts of all conversations and total unread conversation count, you can use this interface.
// Clear the unread message counts of all conversations
zim.clearConversationTotalUnreadMessageCount(new ZIMConversationTotalUnreadMessageCountClearedCallback() {
@Override
public void onConversationTotalUnreadMessageCountCleared(ZIMError errorInfo) {
// Get the operation result
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
}
});