更新时间: 2021-03-08
# 功能描述
用户通过回执消息,可以知道对方是否阅读该消息。
# 效果展示


# 配置说明
属性所属类为 RCIM
,默认为关闭状态。
@property(nonatomic, copy) NSArray* enabledReadReceiptConversationTypeList;
已复制
1
# 代码示例
[RCIM sharedRCIM].enabledReadReceiptConversationTypeList = @[ @(ConversationType_PRIVATE) ];
已复制
1
# 自定义显示
SDK 会话页面中文本消息已读的 UI 默认是一个“对勾”图标,开发者可参考下面内容修改为文本“已读”或“未读”。
思路
:
- 在 cell 显示的时候,将 SDK 默认的图标移除,在对应位置添加文本“已读”或“未读”。
- 需要监听消息发送状态更新的通知,更新 cell。
解决方案
:
下面代码以单聊文本消息为例,如需多更多消息类型修改,可以自行添加判断修改 UI。
- 重写 cell 即将显示的方法,修改 UI
- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath { RCMessageModel *model = [self.conversationDataRepository objectAtIndex:indexPath.row]; if ([cell isKindOfClass:[RCTextMessageCell class]] && model.conversationType == ConversationType_PRIVATE) { if (model.content) { for (UIView *view in [((RCMessageCell *)cell).messageHasReadStatusView subviews]) { [view removeFromSuperview]; } UILabel *hasReadView = [[UILabel alloc] initWithFrame:CGRectMake(-18, 5, 30, 20)]; hasReadView.textAlignment = NSTextAlignmentRight; hasReadView.font = [UIFont systemFontOfSize:12]; hasReadView.textColor = [UIColor blackColor]; ((RCMessageCell *)cell).messageHasReadStatusView.hidden = NO; if (model.sentStatus == SentStatus_READ) { CGRect hasReadViewFrame = hasReadView.frame; hasReadViewFrame.origin.y -= 19; hasReadView.frame = hasReadViewFrame; hasReadView.text = @"已读"; [((RCMessageCell *)cell).messageHasReadStatusView addSubview:hasReadView]; } else if (model.sentStatus == SentStatus_SENT) { hasReadView.text = @"未读"; [((RCMessageCell *)cell).messageHasReadStatusView addSubview:hasReadView]; } } } else { for (UIView *view in [((RCMessageCell *)cell).messageHasReadStatusView subviews]) { [view removeFromSuperview]; } } }
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- 监听消息发送状态更新的通知,执行刷新 cell 的方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageCellUpdateSendingStatusEvent:) name:KNotificationMessageBaseCellUpdateSendingStatus object:nil];
已复制
1
2
3
4
2
3
4
- (void)messageCellUpdateSendingStatusEvent:(NSNotification *)notification { RCMessageCellNotificationModel *notifyModel = notification.object; if (notifyModel && ![notifyModel.actionName isEqualToString:CONVERSATION_CELL_STATUS_SEND_PROGRESS]) { [self reloadMessageCell:notifyModel.messageId]; } }
已复制
1
2
3
4
5
6
2
3
4
5
6
- (void)reloadMessageCell:(long)messageId { __weak typeof(self) __weakself = self; dispatch_async(dispatch_get_main_queue(), ^{ for (int i = 0; i < __weakself.conversationDataRepository.count; i++) { RCMessageModel *model = (__weakself.conversationDataRepository)[i]; if (messageId == model.messageId) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; [__weakself hideCellReceiptView:indexPath withMessageModel:model]; break; } } }); }
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- (void)hideCellReceiptView:(NSIndexPath *)indexPath withMessageModel:(RCMessageModel *)model { UICollectionViewCell *__cell = [self.conversationMessageCollectionView cellForItemAtIndexPath:indexPath]; [self.conversationMessageCollectionView reloadItemsAtIndexPaths:@[ indexPath ]]; //如果是空说明被回收了,重新dequeue一个cell,这里用来解决已读人数闪的问题 if (__cell) { if ([__cell isKindOfClass:[RCMessageCell class]]) { dispatch_async(dispatch_get_main_queue(), ^{ ((RCMessageCell *)__cell).receiptCountLabel.hidden = YES; }); } } }
已复制
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12