Conversation Unread Count
danger
- Conversation message unread counts are stored in localStorage. If the browser does not support or has disabled localStorage, unread message counts will not be saved, and browser page refreshes will cause unread counts to disappear.
- Clearing browser cache may result in inaccurate conversation unread counts.
Get Total Unread Count
Call the getTotalUnreadCount method to retrieve the unread count for all conversation types.
Interface
getTotalUnreadCount(includeMuted, conversationTypes)
#### Parameter Description
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| includeMuted | boolean | No | Whether to include muted conversations (default: false) |
| conversationTypes | [ConversationType]`[]` | No | Conversation types (chatroom and ultra group conversations not supported) |
#### Example Code
```js
const includeMuted = false
const conversationTypes = [RongIMLib.ConversationType.PRIVATE, RongIMLib.ConversationType.GROUP]
RongIMLib.getTotalUnreadCount(includeMuted, conversationTypes).then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
}).catch(error => {
console.log(error)
})
## Get Total Unread Mention Count for All Group Conversations \{#getAllUnreadMentionedCount}
:::tip
- Only supports regular group conversations
- Supported since SDK version 5.9.0
:::
Call the [getAllUnreadMentionedCount](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#getAllUnreadMentionedCount) method to retrieve the total unread mention count for all conversation types.
#### Interface
```js
RongIMLib.getAllUnreadMentionedCount()
#### Parameter Description
None
#### Example Code
```js
RongIMLib.getAllUnreadMentionedCount().then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
}).catch(error => {
console.log(error)
})
## Get Unread Count by Do Not Disturb Level (Web Only) \{#getTotalUnreadCountByLevels}
:::tip
Supported since SDK version 5.5.1
:::
Call the [getTotalUnreadCountByLevels](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#getTotalUnreadCountByLevels) method to find conversations matching the specified Do Not Disturb levels and return the total unread count across all matching conversations.
#### Interface
```js
RongIMLib.getTotalUnreadCountByLevels(conversationTypes, levels)
#### Parameter Description
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversationTypes | [ConversationType]`[]` | No | Conversation types (chatroom not supported) |
| levels | [NotificationLevel]`[]` | No | Do Not Disturb levels (empty array will count unreads for all levels) |
#### Example Code
```js
const conversationTypes = [
RongIMLib.ConversationType.PRIVATE,
RongIMLib.ConversationType.GROUP,
RongIMLib.ConversationType.ULTRA_GROUP,
]
const levels = [
RongIMLib.NotificationLevel.AT_MESSAGE_NOTIFICATION,
RongIMLib.NotificationLevel.AT_USER_NOTIFICATION
]
RongIMLib.getTotalUnreadCountByLevels(conversationTypes, levels).then(res => {
if (res.code === 0) {
console.log(res.data)
} else {
console.log(res.msg)
}
}).catch(error => {
console.log(error)
})
## Get Unread Mention Count by Do Not Disturb Level (Web Only) \{#getTotalUnreadMentionedCountByLevels}
:::tip
Supported since SDK version 5.5.1
:::
Call the [getTotalUnreadMentionedCountByLevels](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#getTotalUnreadMentionedCountByLevels) method to find conversations matching the specified Do Not Disturb levels and return the total unread mention count across all matching conversations.
#### Interface
```js
RongIMLib.getTotalUnreadMentionedCountByLevels(conversationTypes, levels)
#### Parameter Description
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversationTypes | [ConversationType]`[]` | No | Conversation types (chatroom not supported) |
| levels | [NotificationLevel]`[]` | No | Do Not Disturb levels (empty array will count unread mentions for all levels) |
#### Example Code
```js
const conversationTypes = [
RongIMLib.ConversationType.PRIVATE,
RongIMLib.ConversationType.GROUP,
RongIMLib.ConversationType.ULTRA_GROUP,
]
const levels = [
RongIMLib.NotificationLevel.AT_MESSAGE_NOTIFICATION,
RongIMLib.NotificationLevel.AT_USER_NOTIFICATION
]
RongIMLib.getTotalUnreadMentionedCountByLevels(conversationTypes, levels).then(res => {
if (res.code === 0) {
console.log(res.data)
} else {
console.log(res.msg)
}
}).catch(error => {
console.log(error)
})
## Get Specified Conversation Unread Count \{#getUnreadCount}
Call the [getUnreadCount](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#getUnreadCount) method to retrieve the unread count for a specified conversation.
#### Interface
```js
RongIMLib.getUnreadCount(conversation)
#### Parameter Description
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversation | [IConversationOption] | Yes | Target conversation |
#### Example Code
```js
const conversationType = RongIMLib.ConversationType.PRIVATE;
const targetId = "Conversation ID";
RongIMLib.getUnreadCount({ conversationType, targetId }).then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
}).catch(error => {
console.log(error)
})
## Clear Specified Conversation Unread Count \{#clearMessagesUnreadStatus}
Call the [clearMessagesUnreadStatus](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#clearMessagesUnreadStatus) method to clear the unread count for a specified conversation (excluding chatrooms).
#### Interface
```js
RongIMLib.clearMessagesUnreadStatus(conversation)
#### Parameter Description
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversation | [IConversationOption] | Yes | Target conversation |
#### Example Code
```js
const conversationType = RongIMLib.ConversationType.PRIVATE;
const targetId = "Conversation ID";
RongIMLib.clearMessagesUnreadStatus({ conversationType, targetId }).then(res => {
if (res.code === 0) {
console.log(res.code)
} else {
console.log(res.code, res.msg)
}
})
## Clear All Conversation Unread Counts \{#clearAllMessagesUnreadStatus}
:::tip
Electron platform support added in version 5.20.0.
:::
Call the [clearAllMessagesUnreadStatus](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#clearAllMessagesUnreadStatus) method to clear unread counts for all conversations.
#### Interface
```js
RongIMLib.clearAllMessagesUnreadStatus()
#### Parameter Description
None
#### Example Code
```js
const clearRes = await RongIMLib.clearAllMessagesUnreadStatus();
console.info('Result of clearing unread counts for specified conversations:', clearRes);
## Multi-Device Unread Count Synchronization \{#sendSyncReadStatusMessage}
Call the [sendSyncReadStatusMessage](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#sendSyncReadStatusMessage) method to synchronize unread counts across multiple devices.
#### Interface
```js
RongIMLib.sendSyncReadStatusMessage(conversation, timestamp)
#### Parameter Description
| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversation | [IConversationOption] | Yes | Target conversation |
| timestamp | number | Yes | Synchronization timestamp (can be obtained from the message's sendTime) |
#### Example Code
```js
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: 'targetId',
}
// Clear unread count
const { code } = await RongIMLib.clearMessagesUnreadStatus(conversation)
if (code !== 0) {
console.log(`Failed to clear unread count, code: ${code}`);
return;
}
const timestamp = Date.now(); // The sendTime of the read message
// Send read status synchronization message
const syncReadStatusRes = await RongIMLib.sendSyncReadStatusMessage(conversation, timestamp);
console.info('Multi-device read status synchronization:', syncReadStatusRes);
<!-- links -->
[ConversationType]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/enums/ConversationType.html
[NotificationLevel]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/enums/NotificationLevel.html
[IConversationOption]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#IConversationOption