FoxTalk
文档/自定义消息

自定义消息

WuKongIM 协议用整数 contentType 区分消息种类。FoxTalk 在 base 工程预留了若干基础 type, 扩展模块自带的 type 通过 module 注册到 IM service。

lib/src/modules/module_ids.dart 内 ModuleMessageContentTypeIds 集中声明扩展模块用到的 contentType。建议规约:

1 ~ 99
上游 WuKongIM 协议保留 (文本 / 图片 / 语音 / 视频 / 卡片 / 位置 / 文件等)。
100 ~ 999
应用层业务自定义消息。
1000+
自定义业务 (例: groupApprove=1009)。

示例: ModuleMessageContentTypeIds.location = 6, .file = 8, .gifSticker = 3, .lottieSticker = 12, .emojiSticker = 13。新 contentType 必须先确认上游 WKConstant.h 未占用, 避免协议冲突。

WKMessageContent 接口

自定义消息内容继承 wukongimfluttersdk 的 WKMessageContent, 实现 encodeJson / decodeJson, IM service 收发时自动走 SDK 编解码管线。

class WKLocationContent extends WKMessageContent {
  String address = '';
  String title = '';
  double latitude = 0;
  double longitude = 0;

  @override
  Map<String, dynamic> encodeJson() => {
        'address': address,
        'title': title,
        'latitude': latitude,
        'longitude': longitude,
      };

  @override
  WKMessageContent decodeJson(dynamic data) {
    // 从 data 反向填字段...
    return this;
  }
}

注册消息内容 handler

在模块的 register 方法里通过 registerMessageContentHandler 注册 contentType 与 decoder。LocationModule 是典型范例:

registry.registerMessageContentHandler(
  id: ModuleActionIds.messageLocationContent,
  moduleId: id,
  value: const MessageContentRegistration(
    contentType: ModuleMessageContentTypeIds.location,
    decoder: _decodeLocationContent,
  ),
);

WKLocationContent _decodeLocationContent(dynamic data) {
  return WKLocationContent().decodeJson(data) as WKLocationContent;
}

UI 渲染

气泡渲染通过 registerMessageBubbleRenderer 注册 ModuleMessageBubbleRenderer。base 工程的 chat 气泡列表在遍历消息时按 contentType 走 registry, 找不到 renderer 时显示 [未知消息] 兜底。

last updated · 2026-06