Skip to content
能力中心
产品中心
应用市场
WebOffice
开发者后台

发送消息

发送消息

文本消息

接口定义

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;

参数说明

属性类型说明
chatIdString会话 id
textMsgKIMTextMsg文本消息体
notices[KIMNotice]强提醒内容
extraString透传信息
replyMsgIdString被回复的消息 id,非回复消息则为空
cidString消息字符串标识

KIMTextMsg 说明:

属性类型说明
textString文本消息体

KIMNotice 说明:

属性类型说明
typeString强提醒类型。@人的 notice type:"kim-mention"
isAllBool全员强提醒
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;

参数说明

属性类型说明
chatIdString会话 id
imageMsgKIMImageMsg图片消息体
extraString透传信息
replyMsgIdString被回复的消息 id,非回复消息则为空
cidString消息字符串标识

KIMImageMsg 说明:

属性类型说明
localUrlURL选择内置云服务上传图片方式时的文件本地路径
thumbnailUrlURL选择内置云服务上传图片方式时的缩略图文件本地路径
formatString图片格式
nameString图片名称
sizeInt64图片文件大小
widthInt32图片的宽
heightInt32图片的高
storeKeyString图片在云存储上的 key
thumbnailString图片缩略图在云存储上的 key
methodInt32云存储服务商 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;

参数说明

属性类型说明
chatIdString会话 id
voiceMsgKIMVoiceMsg语音消息体
extraString透传信息
cidString消息字符串标识

KIMVoiceMsg 说明:

属性类型说明
storeKeyString语音在云存储上的 key
methodInt32云存储服务商 KIMStoreMethod
sizeInt64语音文件大小
formatString语音格式
codecString语音编解码格式
durationInt64语音时长
sampleRateInt32语音采样率
sampleBitsInt32语音比特率
channelInt32语音声道数
localUrlURL选择内置云服务上传语音方式时的文件本地路径

代码示例

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;

参数说明

属性类型说明
chatIdString会话 id
videoMsgKIMVideoMsg视频消息体
extraString透传信息
cidString消息字符串标识

KIMVideoMsg 说明:

属性类型说明
storeKeyString视频在云存储上的 key
methodInt32云存储服务商 KIMStoreMethod
sizeInt64视频文件大小
formatString视频格式
codecString视频编解码格式
durationInt64视频时长
coverStoreKeyString视频封面在云存储上的 key
coverWidthInt32视频封面宽
coverHeightInt32视频封面高
localUrlURL选择内置云服务上传视频方式时的文件本地路径
coverUrlURL选择内置云服务上传视频方式时封面的文件本地路径

代码示例

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;

参数说明

属性类型说明
chatIdString会话 id
fileMsgKIMFileMsg文件消息体
extraString透传信息
cidString消息字符串标识

KIMFileMsg 说明:

属性类型说明
storeKeyString视频在云存储上的 key
methodInt32云存储服务商 KIMStoreMethod
nameString文件名
sizeInt64文件大小
localUrlURL选择内置云服务上传文件片方式时的文件本地路径

代码示例

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;

参数说明

属性类型说明
chatIdString会话 id
elementMsgs[KIMElementMsg]图文消息元素体
notices[KIMNotice]强提醒内容
extraString透传信息
replyMsgIdString被回复的消息,非回复消息则为空
cidString消息字符串标识

KIMElementMsg 说明:

属性类型说明
tagString元素标识,KIMElementType
textMsgKIMTextMsg文本元素,tag = text 时有效
imgMsgKIMImageMsg图片元素,tag = img 时有效
extraString透传信息

代码示例

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;

参数说明

属性类型说明
chatIdString会话 id
appCustomizeMsgKIMAppCustomizeMsg自定义消息体
notices[KIMNotice]强提醒内容
extraString透传信息
replyMsgIdString被回复的消息,非回复消息则为空
msgConfigKIMMsgConfig消息配置
pushConfigKIMPushConfig推送配置
cidString消息字符串标识

KIMAppCustomizeMsg 说明:

属性类型说明
contentString自定息内容
customizeTypeString消息子类型,由用户自己定义
msgDescString消息描述文本,最大长度 5000 字符,该描述信息用于消息关键词检索及消息摘要显示

KIMPushConfig 说明:

属性类型说明
pushTitleString离线推送时的标题
pushContentString离线推送时的内容
cfgKeyStringupdateUserPushConfigupdateDevicePushConfig配置的key,推送服务将按该key的状态进行推送选择
userPushTypeInt离线推送类型, 当指定人员离线推送时,会取 userList 数组人员去推,KIMUserPushType
userPushValueUserPushValue离线推送操作具体值
iosPushCfgIOSPushConfigiOS 系统的推送配置
androidPushCfgAndroidPushConfigAndroid 系统的推送配置

代码示例

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;

参数说明

参数类型说明
chatIdString消息所在会话 id
cidString要取消的消息 (本地消息)的 cid,发送消息时返回。
successBool操作结果

代码示例

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;

参数说明

参数类型说明
chatIdString消息所在会话 id
cidString要重发的消息 (本地消息)的 cid,发送消息时返回。
sourceHandlerISendMediaMessageCallback原始资源文件回调,仅对资源文件需要预处理但未成功的消息有效,每个 sourceId 必需且仅调用一次 url 回调闭包
successBool操作结果,仅代表进入消息重发流程,重发结果根据消息发送回调判断

代码示例

swift
let success = KIMCore.shared.resendMessage(chatId: chatId, cid: String)

发送状态监听

消息发送结果及接收到新消息将从 KIMMessageDelegate 中返回,注册此代理可监听消息的发送结果。

接口定义

swift
func register(messageDelegate: KIMMessageDelegate)
Objective-C
- (void)registerWithMessageDelegate:(id <KIMMessageDelegate> )messageDelegate;

参数说明

参数类型说明
messageDelegateKIMMessageDelegate消息事件息代理

KIMMessageDelegate 发送消息相关代理方法说明:

方法类型说明
didUpdateMessageSendStatuschatId: String
会话id
cid: String
消息 cid
status: KIMSendStatus
当前状态
sgResp: KIMSendMsgResp?
当 status = success 时返回的消息基本字段信息
error: KIMError?
当 status = failed 时的错误信息
发送消息状态变化时触发
didUpdateMessageTotalProgresschatId: String
会话id
cid: String
消息 cid
progress: Float
子任务进度
发送资源类消息上传总进度

KIMSendStatus 枚举说明:

枚举值说明
success消息发送成功,即已在云端生成对应的消息且收到成功回复
failed消息发送失败,即已经明确发送失败,如请求超时、服务端返回错误等
upload消息文件上传中,仅文件类有此状态,表示当前处于上传过程
sending消息发送中,即请求已发出但尚未收到回复
ready消息已准备但进行发送流程,sdk 对消息发送并发数量进行了限制,当超过最大数量时会有等待队列处于此状态
userCancel用户取消

代码示例

添加监听器

swift
KIMCore.shared.register(messageDelegate: self)