天天看点

使用融云 IM 点击最近聊天记录时跳转到 @ 自己的消息

有没有遇到过这样的问题,在最近聊天记录列表里面有 @ 你的消息,点列表里面对应的记录,进入聊天页面以后,跳到了最新接收到的消息,想要看 @ 自己的消息,还得可劲儿的下来去找,使用体验不好,想要改善的话,往下看。

实现思路就是获取会话中 @ 自己的消息,把这条消息的时间传给聊天页面,然后再跳转,就可以跳转到这条消息了。

  1. 在 push 到会话页面之前,调 RCIMClient 类下面接口,获取 @ 自己的消息
/*! 获取会话中@提醒自己的消息 
@param conversationType    会话类型 
@param targetId            目标会话ID 
@discussion 此方法从本地获取被@提醒的消息(最多返回10条信息) 
@warning 使用 IMKit 注意在进入会话页面前调用,否则在进入会话清除未读数的接口 clearMessagesUnreadStatus: targetId: 以及 设置消息接收状态接口 setMessageReceivedStatus:receivedStatus:会同步清除被提示信息状态。 
*/
- (NSArray *)getUnreadMentionedMessages:(RCConversationType)conversationType targetId:(NSString *)targetId;
           
  1. 遍历得到数组,找到自己想要跳转到的消息,把消息的 sentTime 传给要跳转的聊天页面,再 push 到聊天页面。
/** 进入页面时定位的消息的发送时间 
@discussion 用于消息搜索之后点击进入页面等场景 
*/
@property (nonatomic, assign) long long locatedMessageSentTime;
           

示例代码

- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath {
    if (model.conversationType == ConversationType_GROUP) {
        NSArray *msgs = [[RCIMClient sharedRCIMClient] getUnreadMentionedMessages:model.conversationType targetId:model.targetId];
        if (msgs.count > 0) {
            RCMessage *msg = msgs[0];
            RCConversationViewController *vc = [[RCConversationViewController alloc] initWithConversationType:model.conversationType targetId:model.targetId];
            vc.locatedMessageSentTime = msg.sentTime;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
           

代码接口文档:https://docs.rongcloud.cn/v4/views/im/ui/guide/private/list/event/ios.html#onSelectedTableRow

融云的官网:https://www.rongcloud.cn/

从融云的官网文档能够找到点击会话列表 cell 的回调方法,在该方法里获取 @ 自己的消息,如果有,将该消息的 sentTime 设置给聊天页面对象的 locatedMessageSentTime,再 push。

**注:示例代码中使用的聊天页面是融云 SDK 中的原始类,如果你自己继承了,就替换为你自己的类,别的就没啥了。