please select
  • UIKit
  • SDK
  • Server APIs
Chat/
SDK/
Web/
Conversation/
SDK
  • Install Chat SDK
  • Initialize Chat SDK
  • Login and Logout
  • Client APIs
  • Changelog
  • Message
    • Overview
    • Send a Message
    • Receive a Message
    • Historical Message
    • Forward Messages
    • Modify a Message
    • Delete Messages
    • Clear History Message
    • Recall a Message
    • Send an Online Message
    • Message Read Receipt
    • Query Messages
    • Targeted Group Message
    • Do not Notify
    • Key-Value Extensions
    • Translation
  • Conversation
    • Overview
    • Conversation List
    • Get Conversations
    • Unread Count
    • Pin Conversations
    • Delete Conversations
    • Mark
    • Conversation Group
  • Group
    • Overview
    • Group Management
    • Group Profile
    • Group Member Management
    • Group Member Profile
    • Custom Group Attribute
    • Group Counter
  • Community Topic
    • Community Management
  • User Profile and Relationship Chain
    • User Profile
    • User Status
    • Friend Management
    • Friend Group
    • Block List
  • Guideline for Beginners
  • Console Guide
    • Creating and Upgrading an Application
    • Basic Configuration
    • Feature Configuration
    • Account Management
    • Group Management
    • Webhook Configuration
  • Product Introduction
    • Message Management
      • One-to-One Message
      • Message Storage
      • Offline Push
      • Group Message
      • Message Formats
    • Account System
      • Login Authentication
      • Online Status Management
    • Group Related
      • Group System
      • Group Management
    • User Profile and Relationship Chain
      • Profile Management
      • Relationship Chain Management
  • Purchase Guide
    • Billing Overview
    • Pricing
  • Error Codes

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
}
});