发送消息

支持文本、图片、文件、语音、位置与自定义消息;发送接口统一返回消息实体与发送状态。

平台调用风格

双语言同契约:Swift 用 async -> RcResult<T>(错误码在 RcResult.code,返回 null 的 op 用 RcResult<Void>);Objective-C 用 - (void)opWithParam:... completion:(void (^)(RcResult *))completion,回调在主线程投递。

接口一览​

操作语义传输返回
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 个在本平台绑定中声明了方法签名。

跨端方法名​

操作AndroidWebElectronmacOSFlutterNative (C ABI)iOS
sendMessagesendMessagesendMessagesendMessagesendMessagesendMessageim_client_send_contentfunc
sendTextMessagesendTextMessagesendTextMessagesendTextMessagesendTextMessagesendTextMessageim_client_send_textfunc
sendImageMessagesendImageMessagesendImageMessagesendImageMessagesendImageMessagesendImageMessage—func
sendFileMessagesendFileMessagesendFileMessagesendFileMessagesendFileMessagesendFileMessage—func
sendVoiceMessagesendVoiceMessagesendVoiceMessagesendVoiceMessagesendVoiceMessagesendVoiceMessage—func
sendLocationMessagesendLocationMessagesendLocationMessagesendLocationMessagesendLocationMessagesendLocationMessage—func

方法名在 GA 平台之间保持一致(规则 A);Native C ABI 只覆盖部分操作,其余操作以 — 标注。

sendMessage​

发送任意类型消息。优先走长连接;连接不可用(SUSPEND/DISCONNECTED)时按 SDK_NO_CONNECTION 失败并保留本地待发队列(可选实现)。rev23:options.channelId 用于超级群频道消息(conversationType=10),仅频道成员接收;消息模型带 channelId。

func sendMessage(conversationType: IMPConversationType, targetId: String, content: IMPMessageContent, options: IMPSendOptions?) async -> RcResult<IMPMessage>
WS 长连接since 1.0.0
参数
参数类型说明
conversationTypeConversationType必填;会话类型枚举(ConversationType)
targetIdstring必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId
contentContent必填;消息内容体(按消息类型传对应字段)
options{clientMsgId?:string, channelId?:string}可选;选项对象(字段见类型列):{ clientMsgId?:string, channelId?:string }
返回

Message 消息实体(messageId、seq、发送状态)

示例(Swift / Objective-C)​

let res = await client.sendMessage(conversationType: .privateChat, targetId: "user2", content: .text(IMPTextContent(text: "第一条文本")), options: nil)
[client sendMessageWithConversationType:1 /* 单聊 */ targetId:@"user2" content:@{ @"type": @"text", @"text": @"你好,NI PaaS" } options:nil completion:^(IMPOSResult *result) {
    if (result.code != 0) { NSLog(@"IM 调用失败:%ld %@", (long)result.code, result.msg); }
}];
错误码
错误码名称分类HTTP可重试说明
1001INVALID_PARAMETER客户端 / 参数 / 权限400否参数错误
1003FORBIDDEN客户端 / 参数 / 权限403否无权限
1004USER_NOT_FOUND客户端 / 参数 / 权限404否用户不存在
1005GROUP_NOT_FOUND客户端 / 参数 / 权限404否群组不存在
1006NOT_GROUP_MEMBER客户端 / 参数 / 权限403否未加入群组
1007MESSAGE_TOO_LARGE客户端 / 参数 / 权限400否消息体过大(上限 5KB)
1008RATE_LIMITED客户端 / 参数 / 权限429是发送过于频繁
1009GROUP_MUTED客户端 / 参数 / 权限403否你已被禁言
1010SENSITIVE_WORD客户端 / 参数 / 权限400否内容包含敏感词
1011BLOCKED客户端 / 参数 / 权限403否已被对方拉黑或你已拉黑对方
1012CHATROOM_NOT_FOUND客户端 / 参数 / 权限404否聊天室不存在
1013CHATROOM_CLOSED客户端 / 参数 / 权限410否聊天室已销毁
1014NOT_CHATROOM_MEMBER客户端 / 参数 / 权限403否未加入聊天室
1016USER_BANNED客户端 / 参数 / 权限403否账号已被封禁
1021SERVICE_NOT_ACTIVATEDOpen API 签名与开通403否服务未开通
3001SDK_NOT_CONNECTEDSDK 本地0是长连接未就绪
3002SDK_TIMEOUTSDK 本地0是请求超时

sendTextMessage​

发送文本消息;内部等价 sendMessage(content={type:'text',text,mentionedInfo?,quote?})。

func sendTextMessage(conversationType: IMPConversationType, targetId: String, text: String, options: IMPTextSendOptions?) async -> RcResult<IMPMessage>
WS 长连接since 1.0.0
参数
参数类型说明
conversationTypeConversationType必填;会话类型枚举(ConversationType)
targetIdstring必填;目标会话 ID:单聊为对方 userId,群聊为 groupId,聊天室为 chatroomId
textstring必填;文本内容(上限见消息体限制)
options{clientMsgId?:string, mentionedInfo?:MentionedInfo, quote?:Quote, channelId?:string}可选;选项对象(字段见类型列):{ clientMsgId?:string, mentionedInfo?:MentionedInfo, quote?:Quote, channelId?:string }
返回

Message 消息实体(messageId、seq、发送状态)

示例(Swift / Objective-C)​

let textRes = await client.sendTextMessage(conversationType: .privateChat, targetId: "user2", text: "第二条文本", options: nil)
[client sendTextMessageWithConversationType:1 /* 单聊 */ targetId:@"user2" text:@"你好,NI PaaS" options:nil completion:^(IMPOSResult *result) {
    if (result.code != 0) { NSLog(@"IM 调用失败:%ld %@", (long)result.code, result.msg); }
}];
错误码
错误码名称分类HTTP可重试说明
1001INVALID_PARAMETER客户端 / 参数 / 权限400否参数错误
1007MESSAGE_TOO_LARGE客户端 / 参数 / 权限400否消息体过大(上限 5KB)
1009GROUP_MUTED客户端 / 参数 / 权限403否你已被禁言
1010SENSITIVE_WORD客户端 / 参数 / 权限400否内容包含敏感词
1011BLOCKED客户端 / 参数 / 权限403否已被对方拉黑或你已拉黑对方
3001SDK_NOT_CONNECTEDSDK 本地0是长连接未就绪
3002SDK_TIMEOUTSDK 本地0是请求超时

sendImageMessage​

发送图片消息;url 由 uploadFile 或业务方上传得到。

func sendImageMessage(conversationType: IMPConversationType, targetId: String, content: IMPImageContent, options: IMPSendOptions?) async -> RcResult<IMPMessage>
WS 长连接since 1.0.0
参数
参数类型说明
conversationTypeConversationType必填;会话类型枚举(ConversationType)
targetIdstring必填;目标会话 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、发送状态)

示例(Swift / Objective-C)​

let imageRes = await client.sendImageMessage(conversationType: .privateChat, targetId: "user2", content: IMPImageContent(url: "https://cdn.example.com/a.png"), options: nil)
[client sendImageMessageWithConversationType:1 /* 单聊 */ targetId:@"user2" content:@{ @"url": @"https://cdn.example.com/a.png", @"width": @0, @"height": @0, @"size": @0 } options:nil completion:^(IMPOSResult *result) {
    if (result.code != 0) { NSLog(@"IM 调用失败:%ld %@", (long)result.code, result.msg); }
}];
错误码
错误码名称分类HTTP可重试说明
1001INVALID_PARAMETER客户端 / 参数 / 权限400否参数错误
1007MESSAGE_TOO_LARGE客户端 / 参数 / 权限400否消息体过大(上限 5KB)
1009GROUP_MUTED客户端 / 参数 / 权限403否你已被禁言
1011BLOCKED客户端 / 参数 / 权限403否已被对方拉黑或你已拉黑对方
3001SDK_NOT_CONNECTEDSDK 本地0是长连接未就绪

sendFileMessage​

发送文件消息。

func sendFileMessage(conversationType: IMPConversationType, targetId: String, content: IMPFileContent, options: IMPSendOptions?) async -> RcResult<IMPMessage>
WS 长连接since 1.0.0
参数
参数类型说明
conversationTypeConversationType必填;会话类型枚举(ConversationType)
targetIdstring必填;目标会话 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、发送状态)

示例(Swift / Objective-C)​

let fileRes = await client.sendFileMessage(conversationType: .privateChat, targetId: "user2", content: IMPFileContent(url: "https://cdn.example.com/report.pdf", name: "report.pdf"), options: nil)
[client sendFileMessageWithConversationType:1 /* 单聊 */ targetId:@"user2" content:@{ @"url": @"https://cdn.example.com/a.png", @"name": @"产品群", @"size": @0, @"mime": @"image/png" } options:nil completion:^(IMPOSResult *result) {
    if (result.code != 0) { NSLog(@"IM 调用失败:%ld %@", (long)result.code, result.msg); }
}];
错误码
错误码名称分类HTTP可重试说明
1001INVALID_PARAMETER客户端 / 参数 / 权限400否参数错误
1007MESSAGE_TOO_LARGE客户端 / 参数 / 权限400否消息体过大(上限 5KB)
1009GROUP_MUTED客户端 / 参数 / 权限403否你已被禁言
1011BLOCKED客户端 / 参数 / 权限403否已被对方拉黑或你已拉黑对方
3001SDK_NOT_CONNECTEDSDK 本地0是长连接未就绪

sendVoiceMessage​

发送语音消息;duration 秒 1..300。

func sendVoiceMessage(conversationType: IMPConversationType, targetId: String, content: IMPVoiceContent, options: IMPSendOptions?) async -> RcResult<IMPMessage>
WS 长连接since 1.0.0
参数
参数类型说明
conversationTypeConversationType必填;会话类型枚举(ConversationType)
targetIdstring必填;目标会话 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、发送状态)

示例(Swift / Objective-C)​

let voiceRes = await client.sendVoiceMessage(conversationType: .privateChat, targetId: "user2", content: IMPVoiceContent(url: "https://cdn.example.com/a.m4a", duration: 12), options: nil)
[client sendVoiceMessageWithConversationType:1 /* 单聊 */ targetId:@"user2" content:@{ @"url": @"https://cdn.example.com/a.png", @"duration": @0 } options:nil completion:^(IMPOSResult *result) {
    if (result.code != 0) { NSLog(@"IM 调用失败:%ld %@", (long)result.code, result.msg); }
}];
错误码
错误码名称分类HTTP可重试说明
1001INVALID_PARAMETER客户端 / 参数 / 权限400否参数错误
1007MESSAGE_TOO_LARGE客户端 / 参数 / 权限400否消息体过大(上限 5KB)
1009GROUP_MUTED客户端 / 参数 / 权限403否你已被禁言
1011BLOCKED客户端 / 参数 / 权限403否已被对方拉黑或你已拉黑对方
3001SDK_NOT_CONNECTEDSDK 本地0是长连接未就绪

sendLocationMessage​

发送位置消息;lat/lng 范围校验。

func sendLocationMessage(conversationType: IMPConversationType, targetId: String, content: IMPLocationContent, options: IMPSendOptions?) async -> RcResult<IMPMessage>
WS 长连接since 1.0.0
参数
参数类型说明
conversationTypeConversationType必填;会话类型枚举(ConversationType)
targetIdstring必填;目标会话 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、发送状态)

示例(Swift / Objective-C)​

let locationRes = await client.sendLocationMessage(conversationType: .privateChat, targetId: "user2", content: IMPLocationContent(latitude: 31.23, longitude: 121.47), options: nil)
[client sendLocationMessageWithConversationType:1 /* 单聊 */ targetId:@"user2" content:@{ @"latitude": @0, @"longitude": @0, @"address": @"上海市浦东新区" } options:nil completion:^(IMPOSResult *result) {
    if (result.code != 0) { NSLog(@"IM 调用失败:%ld %@", (long)result.code, result.msg); }
}];
错误码
错误码名称分类HTTP可重试说明
1001INVALID_PARAMETER客户端 / 参数 / 权限400否参数错误
1007MESSAGE_TOO_LARGE客户端 / 参数 / 权限400否消息体过大(上限 5KB)
1009GROUP_MUTED客户端 / 参数 / 权限403否你已被禁言
1011BLOCKED客户端 / 参数 / 权限403否已被对方拉黑或你已拉黑对方
3001SDK_NOT_CONNECTEDSDK 本地0是长连接未就绪

相关文档​