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 queryConversationListWithConfig 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 conversationChanged 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 conversationTotalUnreadMessageCountUpdated 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.
// ......
}
1
// Set up the event handler and listen for related callbacks.
[self.zim setEventHandler:self];
...
// Callback for getting the total number of unread messages.
- (void)zim:(ZIM *)zim conversationTotalUnreadMessageCountUpdated:(unsigned int)totalUnreadMessageCount {
// Get the number of unread messages for UI dispaly.
// ......
}
1
// Set up the event handler and listen for related callbacks.
[self.zim setEventHandler:self];
...
// Callback for getting the total number of unread messages.
- (void)zim:(ZIM *)zim conversationTotalUnreadMessageCountUpdated:(unsigned int)totalUnreadMessageCount {
// Get the number of unread messages for UI dispaly.
// ......
}
1
// Set up the event handler and listen for related callbacks.
zim.setEventHandler(shared_from_this());
//...
// Callback for getting the total number of unread messages.
virtual void onConversationTotalUnreadMessageCountUpdated(ZIM * zim, unsigned int totalUnreadMessageCount) {
// Get the number of unread messages for UI dispaly.
// ......
}
1
zim.on('conversationTotalUnreadMessageCountUpdated', function(zim, { totalUnreadMessageCount }){
// Obtain the total number of unread messages of all conversations and display it on the UI.
})
1
// Callback for getting the total number of unread messages.
ZIMEventHandler.onConversationTotalUnreadMessageCountUpdated =
(totalUnreadMessageCount) {
// Get the number of unread messages for UI dispaly.
// ......
};
1
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 {
// ......
}
}
});
1
// To clear the number of unread messages of specifed conversation.
[self.zim clearConversationUnreadMessageCount:@"CONV_ID" conversationType: ZIMConversationTypePeer callback:^(ZIMError * _Nonnull errorInfo) {
// Returned results of the clear operation.
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
}];
1
// To clear the number of unread messages of specifed conversation.
[self.zim clearConversationUnreadMessageCount:@"CONV_ID" conversationType: ZIMConversationTypePeer callback:^(ZIMError * _Nonnull errorInfo) {
// Returned results of the clear operation.
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
}];
1
// Clear the unread message count of a specified conversation
zim->clearConversationUnreadMessageCount("CONV_ID", ZIMConversationTypePeer, [=](ZIMError errorInfo) {
// Get the result of clearing the unread count
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
});
1
var conversationID = '';
var conversationType = 0;
zim.clearConversationUnreadMessageCount(conversationID, conversationType)
.then(function(res){
// Operation succeeded.
})
.catch(function(err){
// Operation failed.
})
1
ZIM
.getInstance()
.clearConversationUnreadMessageCount(
'conversationID', ZIMConversationType.peer)
.then((value) {})
.catchError((onError) {});
1
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 {
// ......
}
}
});
1
// Clear the unread message counts of all conversations
[self.zim clearConversationTotalUnreadMessageCount:callback:^(ZIMError * _Nonnull errorInfo) {
// Get the operation result
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
}];
1
// Clear the unread message counts of all conversations
[self.zim clearConversationTotalUnreadMessageCount:callback:^(ZIMError * _Nonnull errorInfo) {
// Get the operation result
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
}];
1
// Clear the unread message counts of all conversations
zim->clearConversationTotalUnreadMessageCount([=](ZIMError errorInfo) {
// Get the result of the clear operation
if(errorInfo.code == ZIMErrorCodeSuccess) {
// ......
} else {
// ......
}
});
1
// Clear the unread message counts of all conversations
zim.clearConversationTotalUnreadMessageCount()
.then(function(){
// Operation succeeded
})
.catch(function(err){
// Operation failed
})
1
// Clear the unread message counts of all conversations
ZIM.getInstance()!.clearConversationTotalUnreadMessageCount().
then((value) => {})
.catchError((onError) {});
1