跳到主要内容

会话未读数

危险
  1. 会话消息未读数存储在 localStorage 中, 若浏览器不支持或禁用 localStorage,未读消息数将不会保存,浏览器页面刷新未读消息数将不会存在
  2. 清除浏览器缓存会导致会话未读数不准确。

获取所有会话未读数

调用 getTotalUnreadCount 方法,获取所有会话类型的未读数。

接口

JavaScript
getTotalUnreadCount(includeMuted, conversationTypes)

参数说明

参数类型必填说明
includeMutedboolean是否包含免打扰会话,默认为 false
conversationTypesConversationType[]会话类型,不支持聊天室、超级群会话

示例代码

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

获取所有群会话未读 @ 消息数

提示
  • 仅支持普通群会话
  • SDK 从 5.9.0 版本开始支持该接口

调用 getAllUnreadMentionedCount 方法,获取所有会话类型的未读 @ 消息数。

接口

JavaScript
RongIMLib.getAllUnreadMentionedCount()

参数说明

示例代码

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

按会话免打扰级别获取未读数(仅限 Web 端)

提示

SDK 从 5.5.1 版本开始支持该接口

调用 getTotalUnreadCountByLevels 方法,查找免打扰配置符合传入的免打扰级别的会话,返回所有会话中未读消息的总数。

接口

JavaScript
RongIMLib.getTotalUnreadCountByLevels(conversationTypes, levels)

参数说明

参数类型必填说明
conversationTypesConversationType[]会话类型,不支持聊天室
levelsNotificationLevel[]会话免打扰级别,传空数组则统计全部免打扰级别会话中的未读数

示例代码

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

按会话免打扰级别获取未读 @ 消息数(仅限 Web 端)

提示

SDK 从 5.5.1 版本开始支持该接口

调用 getTotalUnreadMentionedCountByLevels 方法,查找免打扰配置符合传入的免打扰级别的会话,返回所有会话中所有未读 @ 消息的总数。

接口

JavaScript
RongIMLib.getTotalUnreadMentionedCountByLevels(conversationTypes, levels)

参数说明

参数类型必填说明
conversationTypesConversationType[]会话类型,不支持聊天室
levelsNotificationLevel[]会话免打扰级别,传空数组则统计全部免打扰级别会话中的未读数

示例代码

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

获取指定会话未读数

调用 getUnreadCount 方法,获取指定会话未读数。

接口

JavaScript
RongIMLib.getUnreadCount(conversation)

参数说明

参数类型必填说明
conversationIConversationOption目标会话

示例代码

JavaScript
const conversationType = RongIMLib.ConversationType.PRIVATE;
const targetId = " 会话 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)
})

清除指定会话未读数

调用 clearMessagesUnreadStatus 方法,清除指定会话(除聊天室外)的未读数。

接口

JavaScript
RongIMLib.clearMessagesUnreadStatus(conversation)

参数说明

参数类型必填说明
conversationIConversationOption目标会话

示例代码

JavaScript
const conversationType = RongIMLib.ConversationType.PRIVATE;
const targetId = " 会话 Id ";

RongIMLib.clearMessagesUnreadStatus({ conversationType, targetId }).then(res => {
if (res.code === 0) {
console.log(res.code)
} else {
console.log(res.code, res.msg)
}
})

清除全部会话未读数

提示

从 5.20.0 版本开始,该接口新增支持 Electron 平台。

调用 clearAllMessagesUnreadStatus 方法,清除全部会话未读数。

接口

JavaScript
RongIMLib.clearAllMessagesUnreadStatus()

参数说明

示例代码

JavaScript
const clearRes = await RongIMLib.clearAllMessagesUnreadStatus();
console.info('清除指定会话未读数结果:', clearRes);

多端同步未读数

调用 sendSyncReadStatusMessage 方法,可同步多端未读数消息。

接口

JavaScript
 RongIMLib.sendSyncReadStatusMessage(conversation, timestamp)

参数说明

参数类型必填说明
conversationIConversationOption目标会话
timestampnumber同步时间戳,可通过消息的 sendTime 获取

示例代码

JavaScript

const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: 'targetId',
}
// 清除未读数
const { code } = await RongIMLib.clearMessagesUnreadStatus(conversation)
if (code !== 0) {
console.log(`清除未读数失败, code: ${code}`);
return;
}

const timestamp = Date.now(); // 阅读消息的 sendTime
// 发送同步已读消息
const syncReadStatusRes = await RongIMLib.sendSyncReadStatusMessage(conversation, timestamp);
console.info('多端同步阅读状态:', syncReadStatusRes);