
IM即时通讯
安全可靠、全球互通

实时音视频
流畅稳定、省钱省力
回答:
具体开发流程如下(以 2.X SDK 为例):
1、发送 @ 消息
/* @ 消息对象 全部: RongIMLib.MentionedType.ALL 部分: RongIMLib.MentionedType.PART 文档说明: http://support.rongcloud.cn/kb/NjE1 接收 @ 代码: https://rongcloud.github.io/websdk-demo/connect-check.html */ var mentioneds = new RongIMLib.MentionedInfo(); mentioneds.type = RongIMLib.MentionedType.PART; mentioneds.userIdList = ['user1', 'user2']; // @ 用户列表 var isMentioned = true; var content = { content: 'This is a at message', extra: 'extra info', mentionedInfo: mentioneds }; var msg = new RongIMLib.TextMessage(content); RongIMClient.getInstance().sendMessage(3, 'group1', msg, { onSuccess: function(message) { console.log('发送 @ 消息成功', message); }, onError: function(error) { console.log('发送 @ 消息失败', error); } }, isMentioned);
2、接收新消息后, 更新会话列表, 通过会话列表中的会话得知是否 @ 了自己
// 收到消息后, 调用此方法 RongIMClient.getInstance().getConversationList({ onSuccess: function(list) { // list 为会话列表 if (list.length) { var conversation = list[0]; // 获取第一个会话 var mentionedMsg = conversation.mentionedMsg; // 获取 @ 详情 } } });
3、接收新消息并通过 mentioneds 是否包含自己的 id 即可得知是否 @ 了自己
receiveNewMessage: function(message) { var mentionedInfo = message.content.mentionedInfo || {}; var ids = mentionedInfo.userIdList || []; for(var i=0; i < ids.length; i++) { if( ids[i] == userId){ alert("有人 @ 了你!"); } } // 其他消息逻辑 }
参考demo:https://github.com/rongcloud/web-imlib-v2-test
注意:4.X SDK 以上,SDK 内部维护了会话数据中的 hasMentioned (是否包含 @ 自己的消息),不需要进行上述第 3 步的操作,会话所有属性请参考:http://doc.rongcloud.cn/im/Web/4.X/guide/private/conversation/getall/web#remoteparams