addAdvancedMsgListener
监听接收所有类型消息(文本、自定义、富媒体消息),相关回调在 V2TimAdvancedMsgListener
协议中定义。// 创建消息监听器V2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {//单聊已读回调},onRecvMessageModified: (V2TimMessage message) {// msg 为被修改之后的消息对象},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {//群聊已读回调receiptList.forEach((element) {element.groupID; // 群idelement.msgID; // 已读回执消息 IDelement.readCount; // 群消息最新已读数element.unreadCount; // 群消息最新未读数element.userID; // C2C 消息对方 ID});},onRecvMessageRevoked: (String messageid) {// 在本地维护的消息中处理被对方撤回的消息},onRecvNewMessage: (V2TimMessage message) async {// 处理文本消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_TEXT) {message.textElem?.text;}// 使用自定义消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_CUSTOM) {message.customElem?.data;message.customElem?.desc;message.customElem?.extension;}// 使用图片消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_IMAGE) {message.imageElem?.path; // 图片上传时的路径,消息发送者才会有这个字段,消息发送者可用这个字段将图片预先上屏,优化上屏体验。message.imageElem?.imageList?.forEach((element) {// 遍历大图、原图、缩略图// 解析图片属性element?.height;element?.localUrl;element?.size;element?.type; // 大图 缩略图 原图element?.url;element?.uuid;element?.width;});}// 处理视频消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_VIDEO) {// 解析视频消息属性,封面、播放地址、宽高、大小等。message.videoElem?.UUID;message.videoElem?.duration;message.videoElem?.localSnapshotUrl;message.videoElem?.localVideoUrl;message.videoElem?.snapshotHeight;message.videoElem?.snapshotPath;// ...}// 处理音频消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_SOUND) {// 解析语音消息 播放地址,本地地址,大小,时长等。message.soundElem?.UUID;message.soundElem?.dataSize;message.soundElem?.duration;message.soundElem?.localUrl;message.soundElem?.url;// ...}// 处理文件消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FILE) {// 解析文件消息 文件名、文件大小、url等message.fileElem?.UUID;message.fileElem?.fileName;message.fileElem?.fileSize;message.fileElem?.localUrl;message.fileElem?.path;message.fileElem?.url;}// 处理位置消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_LOCATION) {// 解析地理位置消息,经纬度、描述等message.locationElem?.desc;message.locationElem?.latitude;message.locationElem?.longitude;}// 处理表情消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FACE) {message.faceElem?.data;message.faceElem?.index;}// 处理群组tips文本消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS) {message.groupTipsElem?.groupID; // 所属群组message.groupTipsElem?.type; // 群Tips类型message.groupTipsElem?.opMember; // 操作人资料message.groupTipsElem?.memberList; // 被操作人资料message.groupTipsElem?.groupChangeInfoList; // 群信息变更详情message.groupTipsElem?.memberChangeInfoList; // 群成员变更信息message.groupTipsElem?.memberCount; // 当前群在线人数}// 处理合并消息消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_MERGER) {message.mergerElem?.abstractList;message.mergerElem?.isLayersOverLimit;message.mergerElem?.title;V2TimValueCallback<List<V2TimMessage>> download =await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMergerMessage(msgID: message.msgID!,);if (download.code == 0) {List<V2TimMessage>? messageList = download.data;}}if (message.textElem?.nextElem != null) {//通过第一个 Elem 对象的 nextElem 方法获取下一个 Elem 对象,如果下一个 Elem 对象存在,会返回 Elem 对象实例,如果不存在,会返回 null。}},onSendMessageProgress: (V2TimMessage message, int progress) {//文件上传进度回调},);// 添加高级消息的事件监听器TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
// 创建listenerV2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) {},onSendMessageProgress: (V2TimMessage message, int progress) {},);// 注册listenerTencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);// 移除listenerTencentImSDKPlugin.v2TIMManager.getMessageManager().removeAdvancedMsgListener(listener: listener);
addAdvancedMsgListener
设置事件监听器。onRecvNewMessage
(点击查看详情) 回调,在其中接收自定义消息。removeAdvancedMsgListener
移除监听。该步骤不是必须的,客户可以按照业务需求调用。V2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) {// 处理文本消息if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_TEXT){String text = message.textElem.text;}},onSendMessageProgress: (V2TimMessage message, int progress) {},);TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
addAdvancedMsgListener
设置事件监听器。onRecvNewMessage
(点击查看详情) 回调,在其中接收自定义消息。removeAdvancedMsgListener
移除监听。该步骤不是必须的,客户可以按照业务需求调用。V2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) {// 使用自定义消息if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_CUSTOM){String data = message.customElem.data;String desc = message.customElem.desc;String ext = message.customElem.extension;}},onSendMessageProgress: (V2TimMessage message, int progress) {},);TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
addAdvancedMsgListener
接口设置高级消息监听。onRecvNewMessage
(点击查看详情) 获取消息 V2TimMessage。removeAdvancedMsgListener
移除监听。该步骤不是必须的,客户可以按照业务需求调用。V2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) async {// 使用图片消息if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_IMAGE) {String path = message.imageElem.path; // 图片上传时的路径,消息发送者才会有这个字段,消息发送者可用这个字段将图片预先上屏,优化上屏体验。for (var item in message.imageElem.imageList) { // 遍历大图、原图、缩略图// 解析图片属性int height = item.height;String localUrl = item.localUrl;int size = item.size;int type = item.type; // 大图 缩略图 原图String url = item.url;String uuid = item.uuid;int width = item.width;}}// 获取多媒体消息URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // 消息id);if (getMessageOnlineUrlRes.code == 0) {// 获取成功}// 下载多媒体消息V2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // 消息idmessageType: 3, // 消息类型imageType: 0, // 图片类型,仅messageType为图片消息是有效isSnapshot: false // 是否是视频封面,仅messageType为视频消息是有效);if (downloadMessageRes.code == 0) {// 下载成功}},onSendMessageProgress: (V2TimMessage message, int progress) {},);TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_VIDEO){// 解析视频消息属性,封面、播放地址、宽高、大小等。message.videoElem.UUID;message.videoElem.duration;message.videoElem.localSnapshotUrl;message.videoElem.localVideoUrl;message.videoElem.snapshotHeight;message.videoElem.snapshotPath;// ...}// 获取多媒体消息URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // 消息id);if (getMessageOnlineUrlRes.code == 0) {// 获取成功}// 下载多媒体消息V2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // 消息idmessageType: 5, // 消息类型imageType: 0, // 图片类型,仅messageType为图片消息是有效isSnapshot: false // 是否是视频封面,仅messageType为视频消息是有效);if (downloadMessageRes.code == 0) {// 下载成功}
if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_SOUND){// 解析语音消息 播放地址,本地地址,大小,时长等。message.soundElem.UUID;message.soundElem.dataSize;message.soundElem.duration;message.soundElem.localUrl;message.soundElem.url;// ...// 获取多媒体消息URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // 消息id);if (getMessageOnlineUrlRes.code == 0) {// 获取成功}// 下载多媒体消息V2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // 消息idmessageType: 4, // 消息类型imageType: 0, // 图片类型,仅messageType为图片消息是有效isSnapshot: false // 是否是视频封面,仅messageType为视频消息是有效);if (downloadMessageRes.code == 0) {// 下载成功}}
if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FILE){// 解析文件消息 文件名、文件大小、url等message.fileElem.UUID;message.fileElem.fileName;message.fileElem.fileSize;message.fileElem.localUrl;message.fileElem.path;message.fileElem.url;// 获取多媒体消息URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // 消息id);if (getMessageOnlineUrlRes.code == 0) {// 获取成功}// 下载多媒体消息V2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // 消息idmessageType: 6, // 消息类型imageType: 0, // 图片类型,仅messageType为图片消息是有效isSnapshot: false // 是否是视频封面,仅messageType为视频消息是有效);if (downloadMessageRes.code == 0) {// 下载成功}}
locationElem
中解析出经纬度信息。
示例代码向您演示如何从 V2TimMessage 中解析出地理位置消息内容:if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_LOCATION){// 解析地理位置消息,经纬度、描述等message.locationElem.desc;message.locationElem.latitude;message.locationElem.longitude;}
if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FACE) {message.faceElem.data;message.faceElem.index;}
V2TIMGroupTipsElem
中的各个字段V2TIM_GROUP_TIPS_TYPE_GROUP_INFO_CHANGE
,表示这是个群资料变更通知。
接收方可以从 opMember
中获取到操作者信息,再从 groupChangeInfoList
获取到修改后的群名称。
此时,接收方可以将 “操作者” 和 “修改后的群名称” 组装起来,构造出一条群提示。例如 “alice 修改了群名称为 group123”。if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS){message.groupTipsElem.groupID; // 所属群组message.groupTipsElem.type; // 群Tips类型message.groupTipsElem.opMember; // 操作人资料message.groupTipsElem.memberList; // 被操作人资料message.groupTipsElem.groupChangeInfoList; // 群信息变更详情message.groupTipsElem.memberChangeInfoList; // 群成员变更信息message.groupTipsElem.memberCount; // 当前群在线人数}
if(message.textElem.nextElem!=null){// 有下一个消息}
本页内容是否解决了您的问题?