处理会话未读消息数
您可以使用 IMLib SDK 提供的接口直接获取会话中的未读消息数。具体能力如下:
- 获取所有会话(不含聊天室)中的未读消息总数(
getTotalUnreadCountWith
)。 - 获取指定会话中的总未读消息数,或指定会话中指定消息类型的总未读消息数,或按会话类型获取总未读消息总数,或指定会话类型是否包含免打扰消息的总未读消息数(
getUnreadCount
)。
在用户使用您的 App 时,UI 上未读计数可能需要发生变化,此时您可以清除会话中的未读数(clearMessagesUnreadStatus
)。
提示
从 5.3.0 版本 RCCoreClient
开始,建议使用异步返回结果的接口,原同步接口同时废弃。
获取所有会话总未读消息数
您可以使用 getTotalUnreadCountWith
获取所有类型会话(不含聊天室)中未读消息的总数。获取成功后,会返回未读消息数(unreadcount
)。
Objective C
[[RCCoreClient sharedCoreClient] getTotalUnreadCountWith:^(int unreadCount) {
}];
获取指定会话的总未读消息数
您可以获取指定会话中的未读消息总数。获取成功后,会返回未读消息数(count
)。不适用于聊天室、超级群。
Objective C
- (void)getUnreadCount:(RCConversationType)conversationType
targetId:(NSString *)targetId
completion:(nullable void(^)(int count))completion;
参数说明
参数 | 类型 | 说明 |
---|---|---|
conversationType | RCConversationType | 会话类型。不适用于聊天室、超级群。 |
targetId | NSString | 会话 targetId。 |
completion | Block | 异步回调,返回对应的未读数。 |
示例代码
Objective C
[[RCCoreClient sharedCoreClient] getUnreadCount:ConversationType_PRIVATE
targetId:@"targetId"
completion:^(int count) {
}];
获取指定会话中指定消息类型的总消息未读数
提示
- 从 5.1.5 版本开始提供该功能;仅在
RCCoreClient
中提供。
您可以获取指定会话内指定的某一个或多个消息类型的未读数。
接口原型
Objective C
- (void)getUnreadCount:(RCConversationIdentifier *)conversationIdentifier
messageClassList:(NSArray <Class> *)messageClassList
completion:(nullable void(^)(int count))completion;
参数说明
参数 | 类型 | 说明 |
---|---|---|
conversationIdentifier | RCConversationIdentifier | 会话标识,需要指定会话类型(RCConversationType)和 Target ID。 |
messageClassList | NSArray | 消息类型数组,例 @[RCTextMessage.class, RCImageMessage.class] |
completion | Block | 异步回调,返回对应的未读数。 |
示例代码
Objective C
RCConversationIdentifier *iden = [[RCConversationIdentifier alloc] initWithConversationIdentifier:ConversationType_PRIVATE targetId:@"targetId"];
[[RCCoreClient sharedCoreClient] getUnreadCount:iden messageClassList:@[RCTextMessage.class, RCImageMessage.class] completion:^(int count){
//异步回调,返回该会话内的未读消息数
}];
按会话类型获取总未读消息数
提示
- 从 5.20.0 版本开始,支持获取超级群类型的会话未读数(需通过提交工单申请开通)。
您可以获取多个指定会话类型的未读数。获取成功后,会返回未读消息数(count
)。
接口原型
Objective C
- (void)getUnreadCount:(NSArray<NSNumber *> *)conversationTypes
containBlocked:(bool)isContain
completion:(nullable void(^)(int count))completion;
参数说明
参数 | 类型 | 说 明 |
---|---|---|
conversationTypes | NSArray | 会话类型的数组,需要将 RCConversationType 转为 NSNumber 构建 Array。不适用于聊天室、超级群。 |
isContain | BOOL | 是否包含免打扰消息的未读数。 |
completion | Block | 异步回调,返回对应的未读数。 |
示例代码
Objective C
[[RCCoreClient sharedCoreClient] getUnreadCount:@[@(ConversationType_PRIVATE),@(ConversationType_GROUP)]
containBlocked:NO
completion:^(int count) {
}];
按会话免打扰级别获取总未读消息数
提示
- SDK 从 5.2.5 版本开始支持该接口。仅在
RCChannelClient
类中提供。
您可以获取已设置指定免打扰级别的会话的总未读消息数。IMLib SDK 将按照传入的免打扰级别配置查找会话,再返回这些所有会话的总未读消息数。
接口原型
Objective C
- (void)getUnreadCount:(nonnull NSArray <NSNumber*>*)conversationTypes
levels:(nonnull NSArray <NSNumber*>*)levels
success:(nullable void (^)(NSInteger count))successBlock
error:(nullable void (^)(RCErrorCode status))errorBlock;
参数说明
参数 | 类型 | 必填 |
---|---|---|
conversationTypes | RCConversationType [] | 会话类型数组。不适用于聊天室。 |
levels | RCPushNotificationLevel[] | 免打扰类型数组。详见免打扰功能概述。 |
successBlock | Block | 成功回调,返回未读消息数。 |
errorBlock | Block | 失败回调。 |
示例代码
Objective C
NSArray *conversationTypes = @[@(ConversationType_PRIVATE)];
NSArray * pushNotificationLevels = @[@(RCPushNotificationLevelMention)];
[[RCChannelClient sharedChannelManager] getUnreadCount:conversationTypes
levels: pushNotificationLevels
success:^(NSInteger unReadCount) {}
error:^(RCErrorCode status) {}
];
按会话免打扰级别获取总未读 @ 消息数
提示
SDK 从 5.2.5 版本开始支持该接口。仅在 RCChannelClient
类中提供。
您可以获取已设置指定免打扰级别的会话的总未读 @ 消息数。IMLib SDK 将按照传入的免打扰级别配置查找会话,再返回这些所有会话的总未读 @ 消息数。
接口原型
Objective C
- (void)getUnreadMentionedCount:(nonnull NSArray <NSNumber*>*)conversationTypes
levels:(nonnull NSArray <NSNumber*>*)levels
success:(nullable void (^)(NSInteger count))successBlock
error:(nullable void (^)(RCErrorCode status))errorBlock;
参数说明
参数 | 类型 | 必填 |
---|---|---|
conversationTypes | RCConversationType [] | 会话类型数组。不适用于聊天室。 |
levels | RCPushNotificationLevel[] | 免打扰类型数组。详见免打扰功能概述。 |
successBlock | Block | 成功回调,返回未读 @ 消息数。 |
errorBlock | Block | 失败回调。 |
示例方法
Objective C
NSArray *conversationTypes = @[@(ConversationType_GROUP)];
NSArray * pushNotificationLevels = @[@(RCPushNotificationLevelMention)];
[[RCChannelClient sharedChannelManager] getUnreadMentionedCount:conversationTypes
levels: pushNotificationLevels
success:^(NSInteger unReadCount) {}
error:^(RCErrorCode status) {}
];