Unread Count

Feature Description

A user's conversation list usually contains multiple conversations. If there is a new message in one of the conversations, a badge needs to be displayed in the list cell to indicate the unread count. After the user clicks to enter the conversation and goes back to the conversation list, the unread count is cleared, and the badge disappears. In some applications, the total unread count of all the conversations is calculated and displayed at the bottom tab of the conversation list.

Clearing the Conversation Unread Count

API
tim.setMessageRead(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Description
conversationID
String
Conversation ID. Valid values:
C2C${userID} (for a one-to-one chat)
GROUP{groupID} (for a group chat)
@TIM#SYSTEM (for a system notification conversation)
GROUP${topicID} (for a topic)
Returned value
Promise
Sample
// Set all the unread messages in a conversation as read
let promise = chat.setMessageRead({conversationID: 'C2Cexample'});
promise.then(function(imResponse) {
// Set the unread messages as read successfully.
// The value of the `unreadCount` attribute of the conversation with the specified ID is set to `0`.
}).catch(function(imError) {
// Failed to set the unread messages as read
console.warn('setMessageRead error:', imError);
});

Clearing the Unread Count of All Conversations

API
chat.setAllMessageRead(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Description
scope
String | undefined
Set the scope of message processing. Valid values:
TencentCloudChat.TYPES.READ_ALL_C2C_MSG: set the unread messages of all the one-to-one conversations as read
TencentCloudChat.TYPES.READ_ALL_GROUP_MSG: set the unread messages of all the group conversations as read
TencentCloudChat.TYPES.READ_ALL_MSG (default value): set the unread messages of all the one-to-one and group conversations as read
Returned value
Promise
Sample
// Set the unread messages of all the conversations as read
// Same as `chat.setAllMessageRead({scope: TencentCloudChat.TYPES.READ_ALL_MSG})`
let promise = chat.setAllMessageRead();
promise.then(function(imResponse) {
// Set the unread messages as read successfully.
// The values of the `unreadCount` attribute of all the conversations are set to `0`.
}).catch(function(imError) {
// Failed to set the unread messages as read
console.warn('setAllMessageRead error:', imError);
});
// Set the unread messages of all the one-to-one conversations as read
let promise = chat.setAllMessageRead({scope: TencentCloudChat.TYPES.READ_ALL_C2C_MSG});
promise.then(function(imResponse) {
// Set the unread messages as read successfully.
// The values of the `unreadCount` attribute of all the one-to-one conversations are set to `0`.
}).catch(function(imError) {
// Failed to set the unread messages as read
console.warn('setAllMessageRead error:', imError);
});
// Set the unread messages of all the group conversations as read
let promise = chat.setAllMessageRead({scope: TencentCloudChat.TYPES.READ_ALL_GROUP_MSG});
promise.then(function(imResponse) {
// Set the unread messages as read successfully.
// The values of the `unreadCount` attribute of all the group conversations are set to `0`.
}).catch(function(imError) {
// Failed to set the unread messages as read
console.warn('setAllMessageRead error:', imError);
});

Sending a Message Excluded from the Conversation Unread Count

In normal cases, both one-to-one messages and group messages that are sent will be included in the unread count. The unreadCount attribute of the Conversation object indicates the unread message count of a conversation. If you want to send messages that will not be included in the unread count, such as tips or control messages, you can refer to the following code sample.
Sample
// The message control option is supported by v2.16.0 or later.
chat.sendMessage(message, {
messageControlInfo: {
// `unreadCount` of the conversation is not updated (the message is stored on the roaming server).
excludedFromUnreadCount: true,
// `lastMessage` of the conversation is not updated (the message is stored on the roaming server).
excludedFromLastMessage: true
}
});