V2TimMessage.V2TimCustomElem.data
中。如果需要传递的信息量大,建议使用 JSON 格式。class CustomMessage {// 此处的内容请根据需要自行定义String? link;String? text;String? businessID;CustomMessage.fromJSON(Map json) {link = json["link"];text = json["text"];businessID = json["businessID"];}}
CustomMessage? getCustomMessageData(V2TimCustomElem? customElem) {try {if (customElem?.data != null) {final customMessage = jsonDecode(customElem!.data!);return CustomMessage.fromJSON(customMessage);}return null;} catch (err) {return null;}}
TIMUIKitChat
中,使用 messageItemBuilder
内 customMessageItemBuilder
,渲染自定义消息。
示例代码如下:messageItemBuilder: MessageItemBuilder(customMessageItemBuilder: (message, isShowJump, clearJump) {final CustomMessage customMessage = getCustomMessageData(message.customElem);if (linkMessage != null) {final String option1 = linkMessage.link ?? "";return Column(mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.start,children: [Text(linkMessage.text ?? ""),MarkdownBody(data: TIM_t_para("[查看详情 >>]({{option1}})", "[查看详情 >>]($option1)")(option1: option1),styleSheet: MarkdownStyleSheet.fromTheme(ThemeData(textTheme: const TextTheme(bodyText2: TextStyle(fontSize: 16.0)))).copyWith(a: TextStyle(color: LinkUtils.hexToColor("015fff")),),)],);}}),
data
中,并调用 TIMUIKitChatController
的 sendMessage
接口发送。
此处演示示例以直接通过更多功能面板,创建并发送一条自定义消息为例。TIMUIKitChat
中。final TIMUIKitChatController _timuiKitChatController =TIMUIKitChatController();return TIMUIKitChat(controller: _timuiKitChatController,// ...其他参数)
TIMUIKitChat
的 morePanelConfig
属性中的 extraAction
数组新增一项,添加自定义消息发送按钮。
更多功能面板的按钮,主要由文本 title
和图片 icon
组成。
示例代码如下:morePanelConfig: MorePanelConfig(extraAction: [MorePanelItem(id: "customMessage",title: imt("自定义消息"),onTap: (c) {_sendCustomMessage();},icon: Container(height: 64,width: 64,margin: const EdgeInsets.only(bottom: 4),decoration: const BoxDecoration(color: Colors.white,borderRadius: BorderRadius.all(Radius.circular(5))),child: SvgPicture.asset("images/custom-msg.svg",package: 'tencent_cloud_chat_uikit',height: 64,width: 64,),)),// ... 其他更多功能面板按钮],// ...其他参数)
_sendCustomMessage() async {// 创建自定义消息,下方的data/desc/extension可由您自行定义内容。V2TimValueCallback<V2TimMsgCreateInfoResult> createCustomMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createCustomMessage(data:'{"businessID":"text_link","link":"https://www.tencentcloud.com/document/product/269/3794?from_cn_redirect=1","text":"欢迎加入腾讯云IM大家庭!","version":4}',desc: '自定义desc',extension: '自定义extension',);if (createCustomMessageRes.code == 0) {String? id = createCustomMessageRes.data?.id;// 发送自定义消息V2TimValueCallback<V2TimMessage>? sendMessageRes =await _timuiKitChatController.sendMessage(messageInfo: createCustomMessageRes.data?.messageInfo);if (sendMessageRes!.code == 0) {// 发送成功sendMessageRes.data?.customElem?.data; //自定义datasendMessageRes.data?.customElem?.desc; //自定义descsendMessageRes.data?.customElem?.extension; //自定义extension}}}
本页内容是否解决了您的问题?