发送消息
发送消息
文本消息
接口定义
swift
func sendMessage(chatId: String, textMsg: KIMTextMsg, notices: [KIMNotice]? = nil, extra: String? = nil, replyMsgId: String? = nil) -> String
Objective-C
- (NSString *)sendMessageWithChatId:(NSString *)chatId textMsg:(KIMTextMsg *)textMsg notices:(NSArray<KIMNotice *> *)notices extra:(NSString *)extra replyMsgId:(NSString *)replyMsgId;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 id |
textMsg | KIMTextMsg | 文本消息体 |
notices | [KIMNotice] | 强提醒内容 |
extra | String | 透传信息 |
replyMsgId | String | 被回复的消息 id,非回复消息则为空 |
cid | String | 消息字符串标识 |
KIMTextMsg 说明:
属性 | 类型 | 说明 |
---|---|---|
text | String | 文本消息体 |
KIMNotice 说明:
属性 | 类型 | 说明 |
---|---|---|
type | String | 强提醒类型。@人的 notice type:"kim-mention" |
isAll | Bool | 全员强提醒 |
userIds | [String] | 接收强提醒用户标记 |
代码示例
swift
// 发送文本消息,返回值cid为本地消息标记。
let cid = KIMCore.shared.sendMessage(chatId: chatId, textMsg: textMsg, notices: notices, extra: extra, replyMsgId: replyMsgId)
图片消息
接口定义
swift
func sendMessage(chatId: String, imageMsg: KIMImageMsg, extra: String? = nil, replyMsgId: String? = nil) -> String
Objective-C
- (NSString *)sendMessageWithChatId:(NSString *)chatId imageMsg:(KIMImageMsg *)imageMsg extra:(NSString *)extra replyMsgId:(NSString *)replyMsgId;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 id |
imageMsg | KIMImageMsg | 图片消息体 |
extra | String | 透传信息 |
replyMsgId | String | 被回复的消息 id,非回复消息则为空 |
cid | String | 消息字符串标识 |
KIMImageMsg 说明:
属性 | 类型 | 说明 |
---|---|---|
localUrl | URL | 选择内置云服务上传图片方式时的文件本地路径 |
thumbnailUrl | URL | 选择内置云服务上传图片方式时的缩略图文件本地路径 |
format | String | 图片格式 |
name | String | 图片名称 |
size | Int64 | 图片文件大小 |
width | Int32 | 图片的宽 |
height | Int32 | 图片的高 |
storeKey | String | 图片在云存储上的 key |
thumbnail | String | 图片缩略图在云存储上的 key |
method | Int32 | 云存储服务商 KIMStoreMethod |
代码示例
swift
// 发送图片消息,返回值cid为本地消息标记。
let cid = KIMCore.shared.sendMessage(chatId: chatId, imageMsg: imageMsg, extra: extra, replyMsgId: replyMsgId)
语音消息
接口定义
swift
func sendMessage(chatId: String, voiceMsg: KIMVoiceMsg, extra: String? = nil) -> String
Objective-C
- (NSString *)sendMessageWithChatId:(NSString *)chatId voiceMsg:(KIMVoiceMsg *)voiceMsg extra:(NSString *)extra;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 id |
voiceMsg | KIMVoiceMsg | 语音消息体 |
extra | String | 透传信息 |
cid | String | 消息字符串标识 |
KIMVoiceMsg 说明:
属性 | 类型 | 说明 |
---|---|---|
storeKey | String | 语音在云存储上的 key |
method | Int32 | 云存储服务商 KIMStoreMethod |
size | Int64 | 语音文件大小 |
format | String | 语音格式 |
codec | String | 语音编解码格式 |
duration | Int64 | 语音时长 |
sampleRate | Int32 | 语音采样率 |
sampleBits | Int32 | 语音比特率 |
channel | Int32 | 语音声道数 |
localUrl | URL | 选择内置云服务上传语音方式时的文件本地路径 |
代码示例
swift
// 发送语音消息,返回值cid为本地消息标记。
let cid = KIMCore.shared.sendMessage(chatId: chatId, voiceMsg: voiceMsg, extra: extra)
视频消息
接口定义
swift
func sendMessage(chatId: String, videoMsg: KIMVideoMsg, extra: String? = nil) -> String
Objective-C
- (NSString *)sendMessageWithChatId:(NSString *)chatId videoMsg:(KIMVideoMsg *)videoMsg extra:(NSString *)extra;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 id |
videoMsg | KIMVideoMsg | 视频消息体 |
extra | String | 透传信息 |
cid | String | 消息字符串标识 |
KIMVideoMsg 说明:
属性 | 类型 | 说明 |
---|---|---|
storeKey | String | 视频在云存储上的 key |
method | Int32 | 云存储服务商 KIMStoreMethod |
size | Int64 | 视频文件大小 |
format | String | 视频格式 |
codec | String | 视频编解码格式 |
duration | Int64 | 视频时长 |
coverStoreKey | String | 视频封面在云存储上的 key |
coverWidth | Int32 | 视频封面宽 |
coverHeight | Int32 | 视频封面高 |
localUrl | URL | 选择内置云服务上传视频方式时的文件本地路径 |
coverUrl | URL | 选择内置云服务上传视频方式时封面的文件本地路径 |
代码示例
swift
// 发送视频消息,返回值cid为本地消息标记。
let cid = KIMCore.shared.sendMessage(chatId: chatId, videoMsg: videoMsg, extra: extra)
文件消息
接口定义
swift
func sendMessage(chatId: String, fileMsg: KIMFileMsg, extra: String? = nil) -> String
Objective-C
- (NSString *)sendMessageWithChatId:(NSString *)chatId fileMsg:(KIMFileMsg *)fileMsg extra:(NSString *)extra;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 id |
fileMsg | KIMFileMsg | 文件消息体 |
extra | String | 透传信息 |
cid | String | 消息字符串标识 |
KIMFileMsg 说明:
属性 | 类型 | 说明 |
---|---|---|
storeKey | String | 视频在云存储上的 key |
method | Int32 | 云存储服务商 KIMStoreMethod |
name | String | 文件名 |
size | Int64 | 文件大小 |
localUrl | URL | 选择内置云服务上传文件片方式时的文件本地路径 |
代码示例
swift
// 发送文件消息,返回值cid为本地消息标记。
let cid = KIMCore.shared.sendMessage(chatId: chatId, fileMsg: fileMsg, extra: extra)
图文消息
接口定义
swift
func sendMessage(chatId: String, elementMsgs: [KIMElementMsg], notices: [KIMNotice]? = nil, extra: String? = nil, replyMsgId: String? = nil) -> String
Objective-C
- (NSString *)sendMessageWithChatId:(NSString *)chatId elementMsgs:(NSArray<KIMElementMsg *> *)elementMsgs notices:(NSArray<KIMNotice *> *)notices extra:(NSString *)extra replyMsgId:(NSString *)replyMsgId;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 id |
elementMsgs | [KIMElementMsg] | 图文消息元素体 |
notices | [KIMNotice] | 强提醒内容 |
extra | String | 透传信息 |
replyMsgId | String | 被回复的消息,非回复消息则为空 |
cid | String | 消息字符串标识 |
KIMElementMsg 说明:
属性 | 类型 | 说明 |
---|---|---|
tag | String | 元素标识,KIMElementType |
textMsg | KIMTextMsg | 文本元素,tag = text 时有效 |
imgMsg | KIMImageMsg | 图片元素,tag = img 时有效 |
extra | String | 透传信息 |
代码示例
swift
// 发送图文消息,返回值cid为本地消息标记。
let cid = KIMCore.shared.sendMessage(chatId: chatId, elementMsgs: elementMsgs, notices: notices, extra: extra, replyMsgId: replyMsgId)
自定义消息
接口定义
swift
func sendMessage(chatId: String, appCustomizeMsg: KIMAppCustomizeMsg, notices: [KIMNotice]? = nil, extra: String? = nil, replyMsgId: String? = nil, msgConfig: KIMMsgConfig? = nil, pushConfig: KIMPushConfig? = nil) -> String
Objective-C
- (NSString *)sendMessageWithChatId:(NSString *)chatId appCustomizeMsg:(KIMAppCustomizeMsg *)appCustomizeMsg notices:(NSArray<KIMNotice *> *)notices extra:(NSString *)extra replyMsgId:(NSString *)replyMsgId msgConfig:(KIMMsgConfig *)msgConfig pushConfig:(KIMPushConfig *)pushConfig;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 id |
appCustomizeMsg | KIMAppCustomizeMsg | 自定义消息体 |
notices | [KIMNotice] | 强提醒内容 |
extra | String | 透传信息 |
replyMsgId | String | 被回复的消息,非回复消息则为空 |
msgConfig | KIMMsgConfig | 消息配置 |
pushConfig | KIMPushConfig | 推送配置 |
cid | String | 消息字符串标识 |
KIMAppCustomizeMsg 说明:
属性 | 类型 | 说明 |
---|---|---|
content | String | 自定息内容 |
customizeType | String | 消息子类型,由用户自己定义 |
msgDesc | String | 消息描述文本,最大长度 5000 字符,该描述信息用于消息关键词检索及消息摘要显示 |
KIMPushConfig 说明:
属性 | 类型 | 说明 |
---|---|---|
pushTitle | String | 离线推送时的标题 |
pushContent | String | 离线推送时的内容 |
cfgKey | String | updateUserPushConfig 与updateDevicePushConfig 配置的key,推送服务将按该key的状态进行推送选择 |
userPushType | Int | 离线推送类型, 当指定人员离线推送时,会取 userList 数组人员去推,KIMUserPushType |
userPushValue | UserPushValue | 离线推送操作具体值 |
iosPushCfg | IOSPushConfig | iOS 系统的推送配置 |
androidPushCfg | AndroidPushConfig | Android 系统的推送配置 |
代码示例
swift
let cid = KIMCore.shared.sendMessage(chatId: chatId, appCustomizeMsg: appCustomizeMsg, notices: notices, extra: extra, replyMsgId: replyMsgId, msgConfig: msgConfig, pushConfig: pushConfig)
取消发送
对于发送中的消息,若请求尚未发出,用户可以主动取消。建议仅文件类的消息在文件上传时可取消。
接口定义
swift
func cancelMessage(chatId: String, cid: String) -> Bool
Objective-C
- (BOOL)cancelMessageWithChatId:(NSString * _Nonnull)chatId cid:(NSString * _Nonnull)cid;
参数说明
参数 | 类型 | 说明 |
---|---|---|
chatId | String | 消息所在会话 id |
cid | String | 要取消的消息 (本地消息)的 cid,发送消息时返回。 |
success | Bool | 操作结果 |
代码示例
swift
let success = KIMCore.shared.cancelMessage(chatId: chatId, cid: cid)
失败消息重发
接口定义
swift
func resendMessage(chatId: String, cid: String, sourceHandler: KIMSourceCompletionHandle? = nil) -> Bool
Objective-C
- (BOOL)resendMessgeWithChatId:(NSString *)chatId cid:(NSString *)cid sourceHandler:(void (^)(NSString *, NSURL *, void (^)(NSURL *)))sourceHandler;
参数说明
参数 | 类型 | 说明 |
---|---|---|
chatId | String | 消息所在会话 id |
cid | String | 要重发的消息 (本地消息)的 cid,发送消息时返回。 |
sourceHandler | ISendMediaMessageCallback | 原始资源文件回调,仅对资源文件需要预处理但未成功的消息有效,每个 sourceId 必需且仅调用一次 url 回调闭包 |
success | Bool | 操作结果,仅代表进入消息重发流程,重发结果根据消息发送回调判断 |
代码示例
swift
let success = KIMCore.shared.resendMessage(chatId: chatId, cid: String)
发送状态监听
消息发送结果及接收到新消息将从 KIMMessageDelegate
中返回,注册此代理可监听消息的发送结果。
接口定义
swift
func register(messageDelegate: KIMMessageDelegate)
Objective-C
- (void)registerWithMessageDelegate:(id <KIMMessageDelegate> )messageDelegate;
参数说明
参数 | 类型 | 说明 |
---|---|---|
messageDelegate | KIMMessageDelegate | 消息事件息代理 |
KIMMessageDelegate 发送消息相关代理方法说明:
方法 | 类型 | 说明 |
---|---|---|
didUpdateMessageSendStatus | chatId: String 会话id cid: String 消息 cid status: KIMSendStatus 当前状态 sgResp: KIMSendMsgResp? 当 status = success 时返回的消息基本字段信息 error: KIMError? 当 status = failed 时的错误信息 | 发送消息状态变化时触发 |
didUpdateMessageTotalProgress | chatId: String 会话id cid: String 消息 cid progress: Float 子任务进度 | 发送资源类消息上传总进度 |
KIMSendStatus 枚举说明:
枚举值 | 说明 |
---|---|
success | 消息发送成功,即已在云端生成对应的消息且收到成功回复 |
failed | 消息发送失败,即已经明确发送失败,如请求超时、服务端返回错误等 |
upload | 消息文件上传中,仅文件类有此状态,表示当前处于上传过程 |
sending | 消息发送中,即请求已发出但尚未收到回复 |
ready | 消息已准备但进行发送流程,sdk 对消息发送并发数量进行了限制,当超过最大数量时会有等待队列处于此状态 |
userCancel | 用户取消 |
代码示例
添加监听器
swift
KIMCore.shared.register(messageDelegate: self)