今天的干货分享是关于“阅读回执”功能,这是一个很普遍的功能,但是针对使用融云的 SDK 去实现,还是有些坑在等着我们的,下面就开始分(bì)享(kēng)喽~
-
分享之前先做一些准备工作,先找到我们需要调用的接口文档
-
根据不同的会话类型以及消息的发送方和接收方,要分别处理
-
单聊
接收方 :在阅读消息后,调用 RCIMClient 类的发送阅读回执接口,参数如下:
conversationType 单聊会话类型
targetId 消息的会话 ID
time 会话最后一条消息的发送时间(sentTime)
/*! 发送某个会话中消息阅读的回执 @param conversationType 会话类型 @param targetId 会话 ID @param timestamp 该会话中已阅读的最后一条消息的发送时间戳 @param successBlock 发送成功的回调 @param errorBlock 发送失败的回调[nErrorCode: 失败的错误码] @discussion 此接口只支持单聊, 如果使用 IMLib 可以注册监听 RCLibDispatchReadReceiptNotification 通知,使用 IMKit 直接设置RCIM.h 中的 enabledReadReceiptConversationTypeList。 @warning 目前仅支持单聊。 @remarks 高级功能 */ - (void)sendReadReceiptMessage:(RCConversationType)conversationType targetId:(NSString *)targetId time:(long long)timestamp success:(void (^)(void))successBlock error:(void (^)(RCErrorCode nErrorCode))errorBlock;
发送方:监听下面这个通知,在接收后修改消息的展示
/*! @const 收到已读回执的 Notification @discussion 收到消息已读回执之后,IMLib 会分发此通知。 Notification 的 object 为 nil,userInfo 为 NSDictionary 对象, 其中 key 值分别为 @"cType"、@"tId"、@"messageTime", 对应的 value 为会话类型的 NSNumber 对象 、会话的 targetId 、已阅读的最后一条消息的 sendTime。 如: NSNumber *ctype = [notification.userInfo objectForKey:@"cType"]; NSNumber *time = [notification.userInfo objectForKey:@"messageTime"]; NSString *targetId = [notification.userInfo objectForKey:@"tId"]; NSString *fromUserId = [notification.userInfo objectForKey:@"fId"]; 收到这个消息之后可以更新这个会话中 messageTime 以前的消息 UI 为已读(底层数据库消息状态已经改为已读)。 @remarks 事件监听 */ FOUNDATION_EXPORT NSString *const RCLibDispatchReadReceiptNotification;
-
群聊
发送方:
在发送消息 A 后,需要针对该消息发送回执请求,message 传之前发的消息 A
/*! 请求消息阅读回执 @param message 要求阅读回执的消息 @param successBlock 请求成功的回调 @param errorBlock 请求失败的回调[nErrorCode: 失败的错误码] @discussion 通过此接口,可以要求阅读了这条消息的用户发送阅读回执。 @remarks 高级功能 */ - (void)sendReadReceiptRequest:(RCMessage *)message success:(void (^)(void))successBlock error:(void (^)(RCErrorCode nErrorCode))errorBlock;
设置下面代理函数,在接收到发送方发来的阅读回执响应后,修改消息的展示
/*! IMlib消息接收的监听器 @discussion 设置IMLib的消息接收监听器请参考RCIMClient的setReceiveMessageDelegate:object:方法。 @warning 如果您使用IMlib,可以设置并实现此Delegate监听消息接收; 如果您使用IMKit,请使用RCIM中的RCIMReceiveMessageDelegate监听消息接收,而不要使用此监听器,否则会导致IMKit中无法自动更新UI! */ @protocol RCIMClientReceiveMessageDelegate <NSObject> /*! 消息已读回执响应(收到阅读回执响应,可以按照 messageUId 更新消息的阅读数) @param messageUId 请求已读回执的消息ID @param conversationType conversationType @param targetId targetId @param userIdList 已读userId列表 */ - (void)onMessageReceiptResponse:(RCConversationType)conversationType targetId:(NSString *)targetId messageUId:(NSString *)messageUId readerList:(NSMutableDictionary *)userIdList;
接收方:
设置下面代理函数,在接收到消息 A 后,还会接收到针对消息 A 的阅读回执请求
/*! IMlib消息接收的监听器 @discussion 设置IMLib的消息接收监听器请参考RCIMClient的setReceiveMessageDelegate:object:方法。 @warning 如果您使用IMlib,可以设置并实现此Delegate监听消息接收; 如果您使用IMKit,请使用RCIM中的RCIMReceiveMessageDelegate监听消息接收,而不要使用此监听器,否则会导致IMKit中无法自动更新UI! */ @protocol RCIMClientReceiveMessageDelegate <NSObject> /*! 请求消息已读回执(收到需要阅读时发送回执的请求,收到此请求后在会话页面已经展示该 messageUId 对应的消息或者调用 getHistoryMessages 获取消息的时候,包含此 messageUId 的消息,需要调用 sendMessageReadReceiptResponse 接口发送消息阅读回执) @param messageUId 请求已读回执的消息ID @param conversationType conversationType @param targetId targetId */ - (void)onMessageReceiptRequest:(RCConversationType)conversationType targetId:(NSString *)targetId messageUId:(NSString *)messageUId;
在代理方法中,调用下面接口发送阅读回执响应给发送方
/*! 发送阅读回执 @param conversationType 会话类型 @param targetId 会话 ID @param messageList 已经阅读了的消息列表 @param successBlock 发送成功的回调 @param errorBlock 发送失败的回调[nErrorCode: 失败的错误码] @discussion 当用户阅读了需要阅读回执的消息,可以通过此接口发送阅读回执,消息的发送方即可直接知道那些人已经阅读。 @remarks 高级功能 */ - (void)sendReadReceiptResponse:(RCConversationType)conversationType targetId:(NSString *)targetId messageList:(NSArray<RCMessage *> *)messageList success:(void (^)(void))successBlock error:(void (^)(RCErrorCode nErrorCode))errorBlock;
-
总结
阅读回执需要区分会话类型处理,且单聊的阅读回执是针对会话的,群聊的阅读回执是针对某一条消息的:
单聊:接收方阅读某个会话的消息后,发送阅读回执 ——发送方接到阅读回执,更新 UI
群聊:发送方发送消息 A, 针对该消息,发送阅读回执请求 —— 接收方实现监听代理,接收到消息 A 的阅读回执请求 —— 接收方发送阅读回执响应 —— 发送方收到阅读回执响应,更新 UI