消息构建

更新时间: 2025/09/03 10:37:20

网易云信即时通讯 SDK(NetEase IM SDK,以下简称 NIM SDK)支持多种消息类型,助您快速实现多样化的消息业务场景。在进行消息收发等操作前,您需要先构建消息。

本文中消息均指会话内消息,适用于单聊、群组、超大群类型会话。更多消息相关功能请参考开发文档 消息概述

API 概览

API 说明 起始版本
createTextMessage 创建一条文本消息 v10.3.0
createImageMessage 创建一条图片消息 v10.3.0
createAudioMessage 创建一条语音消息 v10.3.0
createVideoMessage 创建一条视频消息 v10.3.0
createFileMessage 创建一条文件消息 v10.3.0
createLocationMessage 创建一条地理位置消息 v10.3.0
createCustomMessage 创建一条自定义消息 v10.3.0
createForwardMessage 创建一条转发消息 v10.3.0
createTipsMessage 创建一条提示消息 v10.3.0
createCallMessage 创建一条话单消息 v10.3.0

接口类

MessageCreator 类提供消息构建接口,支持构建多种类型的消息。

createTextMessage

接口描述

创建一条文本消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createTextMessage(String text) {
  return _platform.createTextMessage(text);
}
参数名称 类型 是否必填 说明
text String 文本消息内容,长度上限为 5000 字符。

示例代码

dartawait MessageCreator.createTextMessage(text);

返回值

已创建的消息对象 NIMMessage

createImageMessage

接口描述

创建一条图片消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createImageMessage(
  String imagePath,
  String? name,
  String? sceneName,
  int width,
  int height, {
  html.File? imageObj,
}) {
  return _platform.createImageMessage(
      imagePath, name, sceneName, width, height,
      imageObj: imageObj);
}
参数名称 类型 是否必填 说明
imagePath String 图片文件地址。
name String 图片文件显示名称,可不同于文件名。
sceneName String NOS 文件存储场景名。若使用自定义的存储场景,需要先调用 addCustomStorageScene 添加自定义存储场景。
width int 图片宽度,单位为像素。
height int 图片高度,单位为像素。
imageObj html.File 图片对象。

示例代码

dartawait MessageCreator.createImageMessage(imagePath, name, sceneName, width, height);

返回值

已创建的消息对象 NIMMessage

createAudioMessage

接口描述

创建一条语音消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createAudioMessage(
  String audioPath,
  String? name,
  String? sceneName,
  int duration, {
  html.File? audioObj,
}) {
  return _platform.createAudioMessage(audioPath, name, sceneName, duration,
      audioObj: audioObj);
}
参数名称 类型 是否必填 说明
audioPath String 语音文件地址。
name String 语音文件显示名称,可不同于文件名。
sceneName String NOS 文件存储场景名。若使用自定义的存储场景,需要先调用 addCustomStorageScene 添加自定义存储场景。
duration int 语音文件时长,单位为毫秒。
audioObj html.File 语音对象。

示例代码

dartawait MessageCreator.createAudioMessage(audioPath, name, sceneName, duration);

返回值

已创建的消息对象 NIMMessage

createVideoMessage

接口描述

创建一条视频消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createVideoMessage(
  String videoPath,
  String? name,
  String? sceneName,
  int duration,
  int width,
  int height, {
  html.File? videoObj,
}) {
  return _platform.createVideoMessage(
      videoPath, name, sceneName, duration, width, height,
      videoObj: videoObj);
}
参数名称 类型 是否必填 说明
videoPath String 视频文件地址。
name String 视频文件显示名称,可不同于文件名。
sceneName String NOS 文件存储场景名。若使用自定义的存储场景,需要先调用 addCustomStorageScene 添加自定义存储场景。
duration int 视频文件时长,单位为毫秒。
width int 视频宽度,单位为像素。
height int 视频高度,单位为像素。
videoObj html.File 视频对象。

示例代码

dartawait MessageCreator.createVideoMessage(videoPath, name, sceneName, duration, width, height);

返回值

已创建的消息对象 NIMMessage

createFileMessage

接口描述

创建一条文件消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createFileMessage(
  String filePath,
  String? name,
  String? sceneName, {
  html.File? fileObj,
}) {
  return _platform.createFileMessage(filePath, name, sceneName,
      fileObj: fileObj);
}
参数名称 类型 是否必填 说明
filePath String 文件地址。
name String 文件显示名称,可不同于文件名。
sceneName String NOS 文件存储场景名。若使用自定义的存储场景,需要先调用 addCustomStorageScene 添加自定义存储场景。
fileObj html.File 文件对象。

示例代码

dartawait MessageCreator.createFileMessage(filePath, name, sceneName);

返回值

已创建的消息对象 NIMMessage

createLocationMessage

接口描述

创建一条地理位置消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createLocationMessage(
    double latitude, double longitude, String address) {
  return _platform.createLocationMessage(latitude, longitude, address);
}
参数名称 类型 是否必填 说明
latitude double 位置维度。
longitude double 位置经度。
address String 位置描述信息。

示例代码

dartawait MessageCreator.createLocationMessage(latitude, longitude, address);

返回值

已创建的消息对象 NIMMessage

createCustomMessage

接口描述

创建一条自定义消息。

  • 已注册一个自定义消息解析器。请参考 自定义消息收发
  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createCustomMessage(
    String text, String rawAttachment) {
  return _platform.createCustomMessage(text, rawAttachment);
}
参数名称 类型 是否必填 说明
text String 自定义消息文本内容,可用于推送及状态栏消息提醒的展示。
rawAttachment String 自定义消息附件对象。SDK 会将 JSON 格式的自定义消息解析为附件对象,用于自定义消息收发。长度限制与各套餐的配置(消息 attach 字段大小上限)有关,具体请参考 字段限制说明

示例代码

dartawait MessageCreator.createCustomMessage(text, rawAttachment);

返回值

已创建的消息对象 NIMMessage

createForwardMessage

接口描述

创建一条转发消息。

  • 转发的消息类型 不能为 notificationrobottipavChat
  • 转发的消息消息必须为发送成功的消息,消息状态必须为 NIMMessagesendingState.succeeded,消息内容与原消息相同。
  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage?>> createForwardMessage(
    NIMMessage message) {
  return _platform.createForwardMessage(message);
}
参数名称 类型 是否必填 说明
message NIMMessage 待转发的消息体。

示例代码

dartawait MessageCreator.createForwardMessage(message);

返回值

已创建的消息对象 NIMMessage

createTipsMessage

接口描述

创建一条提示消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createTipsMessage(String text) {
  return _platform.createTipsMessage(text);
}
参数名称 类型 是否必填 说明
text String 提示消息内容。

示例代码

dartawait MessageCreator.createTipsMessage(text);

返回值

已创建的消息对象 NIMMessage

createCallMessage

接口描述

创建一条话单消息。

  • 在登录后、发送消息前调用该方法。
  • 该方法为同步。

参数说明

dartstatic Future<NIMResult<NIMMessage>> createCallMessage(
    int type,
    String channelId,
    int status,
    List<NIMMessageCallDuration>? durations,
    String? text) {
  return _platform.createCallMessage(
      type, channelId, status, durations, text);
}
参数名称 类型 是否必填 说明
type int 话单类型,业务侧可自定义,网易云信不校验内容。建议设置为:
  • 1:音频
  • 2:视频
  • channelId String 话单频道 ID,网易云信不校验内容。
    status int 通话状态,业务侧可自定义状态,网易云信不校验内容。建议设置为:
  • 1:通话完成
  • 2:通话取消
  • 3:通话拒绝
  • 4:超时未接听
  • 5:对方忙
  • durations List<NIMMessageCallDuration> 通话成员时长列表,网易云信不校验内容。
    text String 话单描述。

    示例代码

    dartawait MessageCreator.createCallMessage(type, channelId, status, durations, text);
    

    返回值

    已创建的消息对象 NIMMessage

    此文档是否对你有帮助?
    有帮助
    去反馈
    • API 概览
    • 接口类
    • createTextMessage
    • createImageMessage
    • createAudioMessage
    • createVideoMessage
    • createFileMessage
    • createLocationMessage
    • createCustomMessage
    • createForwardMessage
    • createTipsMessage
    • createCallMessage