TencentImSDKPlugin.v2TIMManager.getMessageManager()
中。V2TimMessage
。V2TimMessage
中可以携带不同类型子类,表示不同类型的消息。sendMessage
。Future<V2TimValueCallback<V2TimMessage>> sendMessage({required String id,required String receiver,required String groupID,int priority = 0,bool onlineUserOnly = false,bool needReadReceipt = false,bool isExcludedFromUnreadCount = false,bool isExcludedFromLastMessage = false,Map<String, dynamic>? offlinePushInfo,String? cloudCustomData,String? localCustomData,})
参数 | 含义 | 单聊有效 | 群聊有效 | 说明 |
id | 创建消息返回的id | YES | YES | 需要通过对应的 `createXxxMessage` 接口先行创建 |
receiver | 单聊消息接收者 userID | YES | NO | 如果是发送 C2C 单聊消息,只需要指定 receiver 即可 |
groupID | 群聊 groupID | NO | YES | 如果是发送群聊消息,只需要指定 groupID 即可 |
priority | 消息优先级 | NO | YES | 请把重要消息设置为高优先级(例如红包、礼物消息),高频且不重要的消息设置为低优先级(例如点赞消息) |
onlineUserOnly | 是否只有在线用户才能收到 | YES | YES | 如果设置为 YES ,接收方历史消息拉取不到,常被用于实现”对方正在输入”或群组里的非重要提示等弱提示功能 |
offlinePushInfo | 离线推送信息 | YES | YES | 离线推送时携带的标题和内容 |
needReadReceipt | 发送群消息是否支持已读 | NO | YES | 发送群消息是否支持已读 |
isExcludedFromUnreadCount | 发送消息是否计入会话未读数 | YES | YES | 如果设置为 true,发送消息不会计入会话未读,默认为 false |
isExcludedFromLastMessage | 发送消息是否计入会话 lastMessage | YES | YES | 如果设置为 true,发送消息不会计入会话 lastMessage,默认为 false |
cloudCustomData | 消息云端数据 | YES | YES | 消息附带的额外的数据,存云端,消息的接受者可以访问到 |
localCustomData | 消息本地数据 | YES | YES | 消息附带的额外的数据,存本地,消息的接受者不可以访问到,App 卸载后数据丢失 |
sendMessage
。createTextMessage
(点击查看详情) 创建文本消息。sendMessage
(点击查看详情) 发送消息。// 创建文本消息V2TimValueCallback<V2TimMsgCreateInfoResult> createTextMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createTextMessage(text: "test", // 文本信息);if (createTextMessageRes.code == 0) {// 文本信息创建成功String? id = createTextMessageRes.data?.id;// 发送文本消息// 在sendMessage时,若只填写receiver则发个人用户单聊消息// 若只填写groupID则发群组消息// 若填写了receiver与groupID则发群内的个人用户,消息在群聊中显示,只有指定receiver能看见V2TimValueCallback<V2TimMessage> sendMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id!, // 创建的messageidreceiver: "userID", // 接收人idgroupID: "groupID", // 接收群组idpriority: MessagePriorityEnum.V2TIM_PRIORITY_DEFAULT, // 消息优先级onlineUserOnly:false, // 是否只有在线用户才能收到,如果设置为 true ,接收方历史消息拉取不到,常被用于实现“对方正在输入”或群组里的非重要提示等弱提示功能,该字段不支持 AVChatRoom。isExcludedFromUnreadCount: false, // 发送消息是否计入会话未读数isExcludedFromLastMessage: false, // 发送消息是否计入会话 lastMessageneedReadReceipt:false, // 消息是否需要已读回执(只有 Group 消息有效,6.1 及以上版本支持,需要您购买旗舰版套餐)offlinePushInfo: OfflinePushInfo(), // 离线推送时携带的标题和内容cloudCustomData: "", // 消息云端数据,消息附带的额外的数据,存云端,消息的接受者可以访问到localCustomData:"" // 消息本地数据,消息附带的额外的数据,存本地,消息的接受者不可以访问到,App 卸载后数据丢失);if (sendMessageRes.code == 0) {// 发送成功}}
createTextMessage
(点击查看详情) 创建文本消息。sendMessage
(点击查看详情) 发送消息。// 创建文本消息V2TimValueCallback<V2TimMsgCreateInfoResult> createTextMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createTextMessage(text: "test", // 文本信息);if (createTextMessageRes.code == 0) {// 文本信息创建成功String? id = createTextMessageRes.data?.id;// 发送文本消息// 在sendMessage时,若只填写receiver则发个人用户单聊消息// 若只填写groupID则发群组消息// 若填写了receiver与groupID则发群内的个人用户,消息在群聊中显示,只有指定receiver能看见V2TimValueCallback<V2TimMessage> sendMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id!, // 创建的messageidreceiver: "userID", // 接收人idgroupID: "groupID", // 接收群组idpriority: MessagePriorityEnum.V2TIM_PRIORITY_DEFAULT, // 消息优先级onlineUserOnly:false, // 是否只有在线用户才能收到,如果设置为 true ,接收方历史消息拉取不到,常被用于实现“对方正在输入”或群组里的非重要提示等弱提示功能,该字段不支持 AVChatRoom。isExcludedFromUnreadCount: false, // 发送消息是否计入会话未读数isExcludedFromLastMessage: false, // 发送消息是否计入会话 lastMessageneedReadReceipt:false, // 消息是否需要已读回执(只有 Group 消息有效,6.1 及以上版本支持,需要您购买旗舰版套餐)offlinePushInfo: OfflinePushInfo(), // 离线推送时携带的标题和内容cloudCustomData: "", // 消息云端数据,消息附带的额外的数据,存云端,消息的接受者可以访问到localCustomData:"" // 消息本地数据,消息附带的额外的数据,存本地,消息的接受者不可以访问到,App 卸载后数据丢失);if (sendMessageRes.code == 0) {// 发送成功}}
sendMessage
(点击查看详情),比普通接口能设置更多的发送参数(例如优先级、离线推送信息等)。createCustomMessage
(点击查看详情) 创建自定义消息。sendMessage
(点击查看详情) 发送消息。// 创建自定义消息V2TimValueCallback<V2TimMsgCreateInfoResult> createCustomMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createCustomMessage(data: '自定义data',desc: '自定义desc',extension: '自定义extension',);if(createCustomMessageRes.code == 0){String id = createCustomMessageRes.data.id;// 发送自定义消息V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "");if(sendMessageRes.code == 0){// 发送成功}}
createCustomMessage
(点击查看详情) 创建自定义消息。sendMessage
(点击查看详情) 发送消息。// 创建自定义消息V2TimValueCallback<V2TimMsgCreateInfoResult> createCustomMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createCustomMessage(data: '自定义data',desc: '自定义desc',extension: '自定义extension',);if(createCustomMessageRes.code == 0){String id = createCustomMessageRes.data.id;// 发送自定义消息V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "", groupID: "groupID");if(sendMessageRes.code == 0){// 发送成功}}
createXxxMessage
创建指定类型的富媒体消息对象,其中 Xxx 表示具体的消息类型。sendMessage
(点击查看详情) 发送消息。V2TimValueCallback<V2TimMsgCreateInfoResult> createImageMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createImageMessage(imagePath: "本地图片绝对路径",);if (createImageMessageRes.code == 0) {String id = createImageMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}
V2TimValueCallback<V2TimMsgCreateInfoResult> createSoundMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createSoundMessage(soundPath: "本地录音文件绝对路径",duration: 10,// 录音时长);if (createSoundMessageRes.code == 0) {String id = createSoundMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}
V2TimValueCallback<V2TimMsgCreateInfoResult> createVideoMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createVideoMessage(videoFilePath: "本地视频文件绝对路径",type: "mp4", // 视频类型duration: 10,// 视频时长snapshotPath: "本地视频封面文件绝对路径",);if (createVideoMessageRes.code == 0) {String id = createVideoMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}
V2TimValueCallback<V2TimMsgCreateInfoResult> createFileMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createFileMessage(filePath: "本地文件绝对路径",fileName: "文件名",);if (createFileMessageRes.code == 0) {String id = createFileMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}
V2TimValueCallback<V2TimMsgCreateInfoResult> createLocationMessage =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createLocationMessage(desc: "深圳市南山区深南大道",//位置信息摘要longitude: 34,// 经度latitude: 20, // 纬度);if (createLocationMessage.code == 0) {String id = createLocationMessage.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}
V2TimValueCallback<V2TimMsgCreateInfoResult> createFaceMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createFaceMessage(index: 0,data: "",);if (createFaceMessageRes.code == 0) {String id = createFaceMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}
getElementById
的值,若和F12控制台看到 input 的 id 不一致,请以实际为准。final ImagePicker _picker = ImagePicker();_sendImageFileOnWeb() async {final pickedFile = await _picker.pickImage(source: ImageSource.gallery);final imageContent = await pickedFile!.readAsBytes();fileName = pickedFile.name;tempFile = File(pickedFile.path);fileContent = imageContent;html.Node? inputElem;inputElem = html.document.getElementById("__image_picker_web-file-input")?.querySelector("input");final convID = widget.conversationID;final convType =widget.conversationType == 1 ? ConvType.c2c : ConvType.group;final createImageMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createImageMessage(inputElement: inputElement);if (createImageMessageRes.code == 0) {String id = createImageMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}}
final ImagePicker _picker = ImagePicker();_sendVideoFileOnWeb() async {final pickedFile = await _picker.pickVideo(source: ImageSource.gallery);final videoContent = await pickedFile!.readAsBytes();fileName = pickedFile.name ?? "";tempFile = File(pickedFile.path);fileContent = videoContent;if(fileName!.split(".")[fileName!.split(".").length - 1] != "mp4"){Toast.showToast("视频消息仅限 mp4 格式", context);return;}html.Node? inputElem;inputElem = html.document.getElementById("__image_picker_web-file-input")?.querySelector("input");final convID = widget.conversationID;final convType =widget.conversationType == 1 ? ConvType.c2c : ConvType.group;final createVideoMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createVideoMessage(inputElement: inputElement, videoFilePath: "", type: "", duration: 0, snapshotPath: "");if (createVideoMessageRes.code == 0) {String id = createVideoMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}}
_sendFileOnWeb(){final convID = widget.conversationID;final convType =widget.conversationType == 1 ? ConvType.c2c : ConvType.group;FilePickerResult? result = await FilePicker.platform.pickFiles();if (result != null && result.files.isNotEmpty) {html.Node? inputElem;inputElem = html.document.getElementById("__file_picker_web-file-input")?.querySelector("input");fileName = result.files.single.name;final createFileMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createFileMessage(inputElement: inputElement, filePath: "", fileName: fileName);if (createFileMessageRes.code == 0) {String id = createFileMessageRes.data.id;V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(id: id, receiver: "userID", groupID: "groupID");if (sendMessageRes.code == 0) {// 发送成功}}}}
本页内容是否解决了您的问题?