发送消息
支持文本、图片、文件、语音、位置与自定义消息;发送接口统一返回消息实体与发送状态。
平台调用风格
异步风格:Future<RcResult<T>>;错误码在 res.code,不使用异常表达业务失败。
接口一览
| 操作 | 语义 | 传输 | 返回 |
|---|---|---|---|
sendMessage | 发送任意类型消息。优先走长连接;连接不可用(SUSPEND/DISCONNECTED)时按 SDK_NO_CONNECTION 失败并保留本地待发队列(可选实现)。rev23:options.channelId 用于超级群频道消息(conversationType=10),仅频道成员接收;消息模型带 channelId。 | WS 长连接 | Message |
sendTextMessage | 发送文本消息;内部等价 sendMessage(content={type:'text',text,mentionedInfo?,quote?})。 | WS 长连接 | Message |
sendImageMessage | 发送图片消息;url 由 uploadFile 或业务方上传得到。 | WS 长连接 | Message |
sendFileMessage | 发送文件消息。 | WS 长连接 | Message |
sendVoiceMessage | 发送语音消息;duration 秒 1..300。 | WS 长连接 | Message |
sendLocationMessage | 发送位置消息;lat/lng 范围校验。 | WS 长连接 | Message |
本页 6 个操作,其中 6 个在本平台绑定中声明了方法签名。
跨端方法名
| 操作 | Android | Web | Electron | macOS | Flutter | Native (C ABI) | iOS |
|---|---|---|---|---|---|---|---|
sendMessage | sendMessage | sendMessage | sendMessage | sendMessage | sendMessage | im_client_send_content | func |
sendTextMessage | sendTextMessage | sendTextMessage | sendTextMessage | sendTextMessage | sendTextMessage | im_client_send_text | func |
sendImageMessage | sendImageMessage | sendImageMessage | sendImageMessage | sendImageMessage | sendImageMessage | — | func |
sendFileMessage | sendFileMessage | sendFileMessage | sendFileMessage | sendFileMessage | sendFileMessage | — | func |
sendVoiceMessage | sendVoiceMessage | sendVoiceMessage | sendVoiceMessage | sendVoiceMessage | sendVoiceMessage | — | func |
sendLocationMessage | sendLocationMessage | sendLocationMessage | sendLocationMessage | sendLocationMessage | sendLocationMessage | — | func |
方法名在 GA 平台之间保持一致(规则 A);Native C ABI 只覆盖部分操作,其余操作以 — 标注。
sendMessage
发送任意类型消息。优先走长连接;连接不可用(SUSPEND/DISCONNECTED)时按 SDK_NO_CONNECTION 失败并保留本地待发队列(可选实现)。rev23:options.channelId 用于超级群频道消息(conversationType=10),仅频道成员接收;消息模型带 channelId。
sendMessage(int conversationType, String targetId, MessageContent content, {MessageOptions? options})参数
| 参数 | 类型 | 说明 |
|---|---|---|
conversationType | ConversationType | 必填;会话类型枚举(ConversationType) |
targetId | string | 必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId |
content | Content | 必填;消息内容体(按消息类型传对应字段) |
options | {clientMsgId?:string, channelId?:string} | 可选;选项对象(字段见类型列):{ clientMsgId?:string, channelId?:string } |
返回
Message 消息实体(messageId、seq、发送状态)
示例
Dart
final res = await engine.sendMessage(1, 'user2', TextMessageContent(text: '你好,NI PaaS'));
if (res.code != 0) throw Exception(res.msg);错误码
| 错误码 | 名称 | 分类 | HTTP | 可重试 | 说明 |
|---|---|---|---|---|---|
1001 | INVALID_PARAMETER | 客户端 / 参数 / 权限 | 400 | 否 | 参数错误 |
1003 | FORBIDDEN | 客户端 / 参数 / 权限 | 403 | 否 | 无权限 |
1004 | USER_NOT_FOUND | 客户端 / 参数 / 权限 | 404 | 否 | 用户不存在 |
1005 | GROUP_NOT_FOUND | 客户端 / 参数 / 权限 | 404 | 否 | 群组不存在 |
1006 | NOT_GROUP_MEMBER | 客户端 / 参数 / 权限 | 403 | 否 | 未加入群组 |
1007 | MESSAGE_TOO_LARGE | 客户端 / 参数 / 权限 | 400 | 否 | 消息体过大(上限 5KB) |
1008 | RATE_LIMITED | 客户端 / 参数 / 权限 | 429 | 是 | 发送过于频繁 |
1009 | GROUP_MUTED | 客户端 / 参数 / 权限 | 403 | 否 | 你已被禁言 |
1010 | SENSITIVE_WORD | 客户端 / 参数 / 权限 | 400 | 否 | 内容包含敏感词 |
1011 | BLOCKED | 客户端 / 参数 / 权限 | 403 | 否 | 已被对方拉黑或你已拉黑对方 |
1012 | CHATROOM_NOT_FOUND | 客户端 / 参数 / 权限 | 404 | 否 | 聊天室不存在 |
1013 | CHATROOM_CLOSED | 客户端 / 参数 / 权限 | 410 | 否 | 聊天室已销毁 |
1014 | NOT_CHATROOM_MEMBER | 客户端 / 参数 / 权限 | 403 | 否 | 未加入聊天室 |
1016 | USER_BANNED | 客户端 / 参数 / 权限 | 403 | 否 | 账号已被封禁 |
1021 | SERVICE_NOT_ACTIVATED | Open API 签名与开通 | 403 | 否 | 服务未开通 |
3001 | SDK_NOT_CONNECTED | SDK 本地 | 0 | 是 | 长连接未就绪 |
3002 | SDK_TIMEOUT | SDK 本地 | 0 | 是 | 请求超时 |
sendTextMessage
发送文本消息;内部等价 sendMessage(content={type:'text',text,mentionedInfo?,quote?})。
sendTextMessage(int conversationType, String targetId, String text, {MessageOptions? options})参数
| 参数 | 类型 | 说明 |
|---|---|---|
conversationType | ConversationType | 必填;会话类型枚举(ConversationType) |
targetId | string | 必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId |
text | string | 必填;文本内容(上限见消息体限制) |
options | {clientMsgId?:string, mentionedInfo?:MentionedInfo, quote?:Quote, channelId?:string} | 可选;选项对象(字段见类型列):{ clientMsgId?:string, mentionedInfo?:MentionedInfo, quote?:Quote, channelId?:string } |
返回
Message 消息实体(messageId、seq、发送状态)
示例
Dart
final res = await engine.sendTextMessage(1, 'user2', '你好,NI PaaS');
if (res.code != 0) throw Exception(res.msg);错误码
| 错误码 | 名称 | 分类 | HTTP | 可重试 | 说明 |
|---|---|---|---|---|---|
1001 | INVALID_PARAMETER | 客户端 / 参数 / 权限 | 400 | 否 | 参数错误 |
1007 | MESSAGE_TOO_LARGE | 客户端 / 参数 / 权限 | 400 | 否 | 消息体过大(上限 5KB) |
1009 | GROUP_MUTED | 客户端 / 参数 / 权限 | 403 | 否 | 你已被禁言 |
1010 | SENSITIVE_WORD | 客户端 / 参数 / 权限 | 400 | 否 | 内容包含敏感词 |
1011 | BLOCKED | 客户端 / 参数 / 权限 | 403 | 否 | 已被对方拉黑或你已拉黑对方 |
3001 | SDK_NOT_CONNECTED | SDK 本地 | 0 | 是 | 长连接未就绪 |
3002 | SDK_TIMEOUT | SDK 本地 | 0 | 是 | 请求超时 |
sendImageMessage
发送图片消息;url 由 uploadFile 或业务方上传得到。
sendImageMessage(int conversationType, String targetId, ImageContent content, {MessageOptions? options})参数
| 参数 | 类型 | 说明 |
|---|---|---|
conversationType | ConversationType | 必填;会话类型枚举(ConversationType) |
targetId | string | 必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId |
content | {url:string,width?:uint32,height?:uint32,size?:uint64} | 必填;消息内容体(按消息类型传对应字段):{ url:string, width?:uint32, height?:uint32, size?:uint64 } |
options | {clientMsgId?:string, channelId?:string} | 可选;选项对象(字段见类型列):{ clientMsgId?:string, channelId?:string } |
返回
Message 消息实体(messageId、seq、发送状态)
示例
Dart
final res = await engine.sendImageMessage(1, 'user2', ImageContent(url: 'https://cdn.example.com/a.png'));
if (res.code != 0) throw Exception(res.msg);错误码
| 错误码 | 名称 | 分类 | HTTP | 可重试 | 说明 |
|---|---|---|---|---|---|
1001 | INVALID_PARAMETER | 客户端 / 参数 / 权限 | 400 | 否 | 参数错误 |
1007 | MESSAGE_TOO_LARGE | 客户端 / 参数 / 权限 | 400 | 否 | 消息体过大(上限 5KB) |
1009 | GROUP_MUTED | 客户端 / 参数 / 权限 | 403 | 否 | 你已被禁言 |
1011 | BLOCKED | 客户端 / 参数 / 权限 | 403 | 否 | 已被对方拉黑或你已拉黑对方 |
3001 | SDK_NOT_CONNECTED | SDK 本地 | 0 | 是 | 长连接未就绪 |
sendFileMessage
发送文件消息。
sendFileMessage(int conversationType, String targetId, FileContent content, {MessageOptions? options})参数
| 参数 | 类型 | 说明 |
|---|---|---|
conversationType | ConversationType | 必填;会话类型枚举(ConversationType) |
targetId | string | 必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId |
content | {url:string,name:string,size?:uint64,mime?:string} | 必填;消息内容体(按消息类型传对应字段):{ url:string, name:string, size?:uint64, mime?:string } |
options | {clientMsgId?:string, channelId?:string} | 可选;选项对象(字段见类型列):{ clientMsgId?:string, channelId?:string } |
返回
Message 消息实体(messageId、seq、发送状态)
示例
Dart
final res = await engine.sendFileMessage(1, 'user2', FileContent(url: 'https://cdn.example.com/a.png', name: 'report.pdf'));
if (res.code != 0) throw Exception(res.msg);错误码
| 错误码 | 名称 | 分类 | HTTP | 可重试 | 说明 |
|---|---|---|---|---|---|
1001 | INVALID_PARAMETER | 客户端 / 参数 / 权限 | 400 | 否 | 参数错误 |
1007 | MESSAGE_TOO_LARGE | 客户端 / 参数 / 权限 | 400 | 否 | 消息体过大(上限 5KB) |
1009 | GROUP_MUTED | 客户端 / 参数 / 权限 | 403 | 否 | 你已被禁言 |
1011 | BLOCKED | 客户端 / 参数 / 权限 | 403 | 否 | 已被对方拉黑或你已拉黑对方 |
3001 | SDK_NOT_CONNECTED | SDK 本地 | 0 | 是 | 长连接未就绪 |
sendVoiceMessage
发送语音消息;duration 秒 1..300。
sendVoiceMessage(int conversationType, String targetId, VoiceContent content, {MessageOptions? options})参数
| 参数 | 类型 | 说明 |
|---|---|---|
conversationType | ConversationType | 必填;会话类型枚举(ConversationType) |
targetId | string | 必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId |
content | {url:string,duration:uint32} | 必填;消息内容体(按消息类型传对应字段):{ url:string, duration:uint32 } |
options | {clientMsgId?:string, channelId?:string} | 可选;选项对象(字段见类型列):{ clientMsgId?:string, channelId?:string } |
返回
Message 消息实体(messageId、seq、发送状态)
示例
Dart
final res = await engine.sendVoiceMessage(1, 'user2', VoiceContent(url: 'https://cdn.example.com/a.png', duration: 12));
if (res.code != 0) throw Exception(res.msg);错误码
| 错误码 | 名称 | 分类 | HTTP | 可重试 | 说明 |
|---|---|---|---|---|---|
1001 | INVALID_PARAMETER | 客户端 / 参数 / 权限 | 400 | 否 | 参数错误 |
1007 | MESSAGE_TOO_LARGE | 客户端 / 参数 / 权限 | 400 | 否 | 消息体过大(上限 5KB) |
1009 | GROUP_MUTED | 客户端 / 参数 / 权限 | 403 | 否 | 你已被禁言 |
1011 | BLOCKED | 客户端 / 参数 / 权限 | 403 | 否 | 已被对方拉黑或你已拉黑对方 |
3001 | SDK_NOT_CONNECTED | SDK 本地 | 0 | 是 | 长连接未就绪 |
sendLocationMessage
发送位置消息;lat/lng 范围校验。
sendLocationMessage(int conversationType, String targetId, LocationContent content, {MessageOptions? options})参数
| 参数 | 类型 | 说明 |
|---|---|---|
conversationType | ConversationType | 必填;会话类型枚举(ConversationType) |
targetId | string | 必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId |
content | {latitude:double,longitude:double,address?:string} | 必填;消息内容体(按消息类型传对应字段):{ latitude:double, longitude:double, address?:string } |
options | {clientMsgId?:string, channelId?:string} | 可选;选项对象(字段见类型列):{ clientMsgId?:string, channelId?:string } |
返回
Message 消息实体(messageId、seq、发送状态)
示例
Dart
final res = await engine.sendLocationMessage(1, 'user2', LocationContent(latitude: 0, longitude: 0));
if (res.code != 0) throw Exception(res.msg);错误码
| 错误码 | 名称 | 分类 | HTTP | 可重试 | 说明 |
|---|---|---|---|---|---|
1001 | INVALID_PARAMETER | 客户端 / 参数 / 权限 | 400 | 否 | 参数错误 |
1007 | MESSAGE_TOO_LARGE | 客户端 / 参数 / 权限 | 400 | 否 | 消息体过大(上限 5KB) |
1009 | GROUP_MUTED | 客户端 / 参数 / 权限 | 403 | 否 | 你已被禁言 |
1011 | BLOCKED | 客户端 / 参数 / 权限 | 403 | 否 | 已被对方拉黑或你已拉黑对方 |
3001 | SDK_NOT_CONNECTED | SDK 本地 | 0 | 是 | 长连接未就绪 |
相关文档
- 功能列表:103 个操作与签名
- 状态码:错误码与重试策略
- API 参考:类与成员
- IMLib 集成指南