跳到主要内容

获取未读消息数

超级群业务支持从客户端 SDK 获取未读消息数,具体如下:

  • 当前用户加入的所有超级群、指定超级群、或指定频道未读消息数。
  • 当前用户加入的所有超级群、指定超级群、或指定频道的未读 @ 消息数。

返回的未读数最大值为 999。如果实际未读数超过 999,接口仍返回 999。

获取所有会话的未读消息数

通过 getAllUltraGroupUnreadCount 方法,获取当前用户加入的所有超级群会话的未读消息数的总和。

接口

JavaScript
RongIMLib.getAllUltraGroupUnreadCount()

参数说明

示例代码

javascript
RongIMLib.getAllUltraGroupUnreadCount().then((res) => {
if (res.code === 0) {
console.log(res.data)
} else {
console.log(res.code, res.msg)
}
})

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

通过 getAllUltraGroupUnreadMentionedCount 方法,获取当前用户加入的所有超级群会话中的未读 @ 消息数的总和。

接口

JavaScript
RongIMLib.getAllUltraGroupUnreadMentionedCount()

参数说明

示例代码

javascript
RongIMLib.getAllUltraGroupUnreadMentionedCount().then((res) => {
if (res.code === 0) {
console.log(res.data)
} else {
console.log(res.code, res.msg)
}
})

获取指定会话的未读消息数

通过 getUltraGroupUnreadCountByTargetId 方法,获取当前用户在指定超级群会话中的未读消息数。

接口

JavaScript
RongIMLib.getUltraGroupUnreadCountByTargetId(targetId, notificationLevels)

参数说明

参数类型必填说明
targetIdString超级群 ID
notificationLevelsNotificationLevel[]会话的免打扰级别设置。SDK 会根据该参数查找匹配的会话,并计算未读消息数。5.5.1 版本开始支持

示例代码

javascript
const targetId = '超级群 ID'

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

获取指定会话的未读 @ 消息数

通过 getUltraGroupUnreadMentionedCountByTargetId 方法,获取当前用户在指定超级群会话中的未读 @ 消息数。

接口

JavaScript
RongIMLib.getUltraGroupUnreadMentionedCountByTargetId(targetId, notificationLevels)

参数说明

参数类型必填说明
targetIdString超级群 ID
notificationLevelsNotificationLevel[]会话的免打扰级别设置。SDK 会根据该参数查找匹配的会话,并计算未读消息数。5.5.1 版本开始支持

示例代码

javascript
const targetId = '超级群 ID'

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

获取指定频道的未读消息数

通过 getUnreadCount 方法,获取当前用户在超级群会话指定的频道中的未读消息数。

接口

JavaScript
RongIMLib.getUnreadCount(conversation)

参数说明

提示

获取指定频道的未读消息数要求必须传入超级群频道 ID(channelId

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

示例代码

javascript
const conversation = {
conversationType: RongIMLib.ConversationType.ULTRA_GROUP,
targetId: '超级群 ID',
channelId: '超级群频道 ID',
}

RongIMLib.getUnreadCount(conversation)
.then((res) => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
})
.catch((error) => {
console.log(error)
})

获取指定频道 @ 未读数

通过 getUnreadMentionedCount 方法,获取当前用户在指定超级群会话指定频道的未读 @ 消息数。

接口

JavaScript
RongIMLib.getUnreadMentionedCount(conversation)

参数说明

提示

获取指定频道的未读消息数要求必须传入超级群频道 ID(channelId

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

示例代码

javascript
const conversation = {
conversationType: RongIMLib.ConversationType.ULTRA_GROUP,
targetId: '超级群 ID',
channelId: '超级群频道 ID',
}

RongIMLib.getUnreadMentionedCount(conversation)
.then((res) => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
})
.catch((error) => {
console.log(error)
})