V2TIMManager
和 V2TIMMessageManager(Android)
/ V2TIMManager(Message)(iOS & Mac)
中。V2TIMMessage
。V2TIMMessage
中可以携带 V2TIMElem
的不同类型子类,表示不同类型的消息。sendMessage
。// V2TIMMessageManagerpublic abstract String sendMessage(V2TIMMessage message,String receiver,String groupID,int priority,boolean onlineUserOnly,V2TIMOfflinePushInfo offlinePushInfo,V2TIMSendCallback<V2TIMMessage> callback);
参数 | 含义 | 单聊有效 | 群聊有效 | 说明 |
message | 消息对象 | YES | YES | 需要通过对应的 `createXxxMessage` 接口先行创建,`Xxx` 表示具体的类型。 |
receiver | 单聊消息接收者 userID | YES | NO | 如果是发送 C2C 单聊消息,只需要指定 `receiver` 即可。 |
groupID | 群聊 groupID | NO | YES | 如果是发送群聊消息,只需要指定 `groupID` 即可。 |
priority | 消息优先级 | NO | YES | 请把重要消息设置为高优先级(例如红包、礼物消息),高频且不重要的消息设置为低优先级(例如点赞消息)。 |
onlineUserOnly | 是否只有在线用户才能收到 | YES | YES | 如果设置为 `true`,接收方历史消息拉取不到,常被用于实现 “对方正在输入” 或群组里的非重要提示等弱提示功能。 |
offlinePushInfo | 离线推送信息 | YES | YES | 离线推送时携带的标题和内容。 |
callback | 发送回调 | YES | YES | 包含上传进度回调、发送成功回调、发送失败回调。 |
// V2TIMManager+Message.h- (NSString *)sendMessage:(V2TIMMessage *)messagereceiver:(NSString *)receivergroupID:(NSString *)groupIDpriority:(V2TIMMessagePriority)priorityonlineUserOnly:(BOOL)onlineUserOnlyofflinePushInfo:(V2TIMOfflinePushInfo *)offlinePushInfoprogress:(V2TIMProgress)progresssucc:(V2TIMSucc)succfail:(V2TIMFail)fail;
参数 | 含义 | 单聊有效 | 群聊有效 | 说明 |
message | 消息对象 | YES | YES | 需要通过对应的 `createXxxMessage` 接口先行创建,`Xxx` 表示具体的类型。 |
receiver | 单聊消息接收者 userID | YES | NO | 如果是发送 C2C 单聊消息,只需要指定 `receiver` 即可。 |
groupID | 群聊 groupID | NO | YES | 如果是发送群聊消息,只需要指定 `groupID` 即可。 |
priority | 消息优先级 | NO | YES | 请把重要消息设置为高优先级(例如红包、礼物消息),高频且不重要的消息设置为低优先级(例如点赞消息)。 |
onlineUserOnly | 是否只有在线用户才能收到 | YES | YES | 如果设置为 `YES`,接收方历史消息拉取不到,常被用于实现 “对方正在输入” 或群组里的非重要提示等弱提示功能。 |
offlinePushInfo | 离线推送信息 | YES | YES | 离线推送时携带的标题和内容。 |
progress | 文件上传进度 | YES | YES | 文件上传进度。当发送消息中包含图片、语音、视频、文件等富媒体消息时才有效,纯文本、表情、定位消息不会回调。 |
succ | 消息发送成功回调 | YES | YES | —— |
fail | 消息发送失败回调 | YES | YES | 回调失败错误码、错误描述。 |
// V2TIMMessageManagervirtual V2TIMString SendMessage(V2TIMMessage& message,const V2TIMString& receiver,const V2TIMString& groupID,V2TIMMessagePriority priority,bool onlineUserOnly,const V2TIMOfflinePushInfo& offlinePushInfo,V2TIMSendCallback* callback);
参数 | 含义 | 单聊有效 | 群聊有效 | 说明 |
message | 消息对象 | YES | YES | 需要通过对应的 CreateXxxMessage 接口先行创建,Xxx 表示具体的类型。 |
receiver | 单聊消息接收者 userID | YES | NO | 如果是发送 C2C 单聊消息,只需要指定 receiver 即可。 |
groupID | 群聊 groupID | NO | YES | 如果是发送群聊消息,只需要指定 groupID 即可。 |
priority | 消息优先级 | NO | YES | 请把重要消息设置为高优先级(例如红包、礼物消息),高频且不重要的消息设置为低优先级(例如点赞消息)。 |
onlineUserOnly | 是否只有在线用户才能收到 | YES | YES | 如果设置为 true ,接收方历史消息拉取不到,常被用于实现 “对方正在输入” 或群组里的非重要提示等弱提示功能。 |
offlinePushInfo | 离线推送信息 | YES | YES | 离线推送时携带的标题和内容。 |
callback | 发送回调 | YES | YES | 包含上传进度回调、发送成功回调、发送失败回调。 |
sendMessage
。// API 返回 msgID,按需使用String msgID = V2TIMManager.getInstance().sendC2CTextMessage("单聊文本消息", "receiver_userID", new V2TIMValueCallback<V2TIMMessage>() {@Overridepublic void onSuccess(V2TIMMessage message) {// 发送单聊文本消息成功}@Overridepublic void onError(int code, String desc) {// 发送单聊文本消息失败}});
// API 返回 msgID,按需使用NSString *msgID = [[V2TIMManager sharedInstance] sendC2CTextMessage:@"单聊文本消息"to:@"receiver_userID"succ:^{// 发送单聊文本消息成功} fail:^(int code, NSString *msg) {// 发送单聊文本消息失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 发送单聊文本消息成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 发送单聊文本消息失败delete callback;},[=](uint32_t progress) {// 文本消息不会回调进度});// API 返回 msgID,按需使用V2TIMString msgID =V2TIMManager::GetInstance()->SendC2CTextMessage("单聊文本消息", "receiver_userID", callback);
// 创建文本消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createTextMessage("content");// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 文本消息不会回调进度}@Overridepublic void onSuccess(V2TIMMessage message) {// 文本消息发送成功}@Overridepublic void onError(int code, String desc) {// 文本消息发送失败}});
// 创建文本消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createTextMessage:@"content"];// 发送消息[V2TIMManager.sharedInstance sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_NORMALonlineUserOnly:NOofflinePushInfo:nilprogress:nilsucc:^{// 文本消息发送成功}fail:^(int code, NSString *desc) {// 文本消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建文本消息V2TIMMessage v2TIMMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage("content");// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 文本消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 文本消息发送失败delete callback;},[=](uint32_t progress) {// 文本消息不会回调进度});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
V2TIMMessagePriority
定义。// API 返回 msgID,按需使用String msgID = V2TIMManager.getInstance().sendGroupTextMessage("群聊文本消息", "groupID", V2TIMMessage.V2TIM_PRIORITY_NORMAL, new V2TIMValueCallback<V2TIMMessage>() {@Overridepublic void onSuccess(V2TIMMessage message) {// 发送群聊文本消息成功}@Overridepublic void onError(int code, String desc) {// 发送群聊文本消息失败}});
// API 返回 msgID,按需使用NSString *msgID = [[V2TIMManager sharedInstance] sendGroupTextMessage:@"群聊文本消息"to:@"groupID" // 群聊的 groupIDpriority:V2TIM_PRIORITY_NORMAL // 消息优先级succ:^{// 群聊文本消息发送成功} fail:^(int code, NSString *msg) {// 群聊文本消息发送失败}];
class SendGroupTextMessageCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendGroupTextMessageCallback() = default;~SendGroupTextMessageCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallbackprogress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};auto callback = new SendGroupTextMessageCallback;callback->SetCallback([=](const V2TIMMessage& message) {// 发送群聊文本消息成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 发送群聊文本消息失败delete callback;},[=](uint32_t progress) {// 文本消息不会回调进度});// API 返回 msgID,按需使用V2TIMString msgID = V2TIMManager::GetInstance()->SendGroupTextMessage("群聊文本消息", "groupID", V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, callback);
// 创建文本消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createTextMessage("content");// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, null, "receiver_groupID", V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 文本消息不会回调进度}@Overridepublic void onSuccess(V2TIMMessage message) {// 发送群聊文本消息成功}@Overridepublic void onError(int code, String desc) {// 发送群聊文本消息失败}});
// 创建文本消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createTextMessage:content];// 发送消息[V2TIMManager.sharedInstance sendMessage:messagereceiver:nilgroupID:@"receiver_groupID" // 群聊的 groupIDpriority:V2TIM_PRIORITY_NORMAL // 消息优先级onlineUserOnly:NO // 仅在线用户接收offlinePushInfo:nil // 离线推送自定义信息progress:nilsucc:^{// 文本消息发送成功}fail:^(int code, NSString *desc) {// 文本消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建文本消息V2TIMMessage v2TIMMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage("content");// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 发送群聊文本消息成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 发送群聊文本消息失败delete callback;},[=](uint32_t progress) {// 文本消息不会回调进度});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, {}, "receiver_groupID", V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
sendMessage
(Android / iOS & Mac / Windows),比普通接口能设置更多的发送参数(例如优先级、离线推送信息等)。String msgID = V2TIMManager.getInstance().sendC2CCustomMessage("单聊自定义消息".getBytes(), "receiver_userID", new V2TIMValueCallback<V2TIMMessage>() {@Overridepublic void onSuccess(V2TIMMessage message) {// 发送单聊自定义消息成功}@Overridepublic void onError(int code, String desc) {// 发送单聊自定义消息失败}});
NSData *customData = [@"单聊自定义消息" dataUsingEncoding:NSUTF8StringEncoding];NSString *msgID = [[V2TIMManager sharedInstance] sendC2CCustomMessage:customDatato:@"receiver_userID" // 接收者 userIDsucc:^{// 单聊自定义消息发送成功}fail:^(int code, NSString *msg) {// 单聊自定义消息发送失败}];
class SendC2CCustomMessageCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendC2CCustomMessageCallback() = default;~SendC2CCustomMessageCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 发送单聊自定义消息成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 发送单聊自定义消息失败delete callback;},[=](uint32_t progress) {// 自定义消息不会回调进度});V2TIMString str = u8"单聊自定义消息";V2TIMBuffer customData = {reinterpret_cast<const uint8_t*>(str.CString()), str.Size()};V2TIMString msgID = V2TIMManager::GetInstance()->SendC2CCustomMessage(customData, "receiver_userID", callback);
// 创建自定义消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createCustomMessage("单聊自定义消息".getBytes());// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 自定义消息不会回调进度}@Overridepublic void onSuccess(V2TIMMessage message) {// 发送单聊自定义消息成功}@Overridepublic void onError(int code, String desc) {// 发送单聊自定义消息失败}});
V2TIMMessage *message = [[V2TIMManager sharedInstance] createCustomMessage:data];[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"receiver_userID" // 接收者 userIDgroupID:nilpriority:V2TIM_PRIORITY_DEFAULT // 消息优先级onlineUserOnly:NOofflinePushInfo:nilprogress:nilsucc:^{// 单聊自定义消息发送成功} fail:^(int code, NSString *desc) {// 单聊自定义消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建自定义消息V2TIMString str = u8"单聊自定义消息";V2TIMBuffer customData = {reinterpret_cast<const uint8_t*>(str.CString()), str.Size()};V2TIMMessage v2TIMMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateCustomMessage(customData);// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 发送单聊自定义消息成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 发送单聊自定义消息失败delete callback;},[=](uint32_t progress) {// 自定义消息不会回调进度});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
sendGroupCustomMessage
(Android / iOS & Mac / Windows) 发送群聊自定义消息,直接传递消息二进制内容、群聊 groupID 和优先级即可。
消息优先级可参考 V2TIMMessagePriority
定义。String msgID = V2TIMManager.getInstance().sendGroupCustomMessage("群聊自定义消息".getBytes(), "groupID", V2TIMMessage.V2TIM_PRIORITY_NORMAL, new V2TIMValueCallback<V2TIMMessage>() {@Overridepublic void onSuccess(V2TIMMessage message) {// 发送群聊自定义消息成功}@Overridepublic void onError(int code, String desc) {// 发送群聊自定义消息失败}});
NSData *customData = [@"群聊自定义消息" dataUsingEncoding:NSUTF8StringEncoding];NSString *msgID = [[V2TIMManager sharedInstance] sendGroupCustomMessage:customDatato:@"receiver_groupID" // 群聊的 groupIDpriority:V2TIM_PRIORITY_HIGH // 消息优先级succ:^{// 群聊自定义消息发送成功} fail:^(int code, NSString *msg) {// 群聊自定义消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 发送群聊自定义消息成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 发送群聊自定义消息失败delete callback;},[=](uint32_t progress) {// 自定义消息不会回调进度});V2TIMString str = u8"群聊自定义消息";V2TIMBuffer customData = {reinterpret_cast<const uint8_t*>(str.CString()), str.Size()};V2TIMString msgID = V2TIMManager::GetInstance()->SendGroupCustomMessage(customData, "groupID", V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, callback);
// 创建自定义消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createCustomMessage("群聊自定义消息".getBytes());// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, null, "receiver_groupID", V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 自定义消息不会回调进度}@Overridepublic void onSuccess(V2TIMMessage message) {// 发送群聊自定义消息成功}@Overridepublic void onError(int code, String desc) {// 发送群聊自定义消息失败}});
V2TIMMessage *message = [[V2TIMManager sharedInstance] createCustomMessage:data];[[V2TIMManager sharedInstance] sendMessage:messagereceiver:nilgroupID:@"receiver_groupID" // 群聊的 groupIDpriority:V2TIM_PRIORITY_DEFAULT // 消息优先级onlineUserOnly:NOofflinePushInfo:nilprogress:nilsucc:^{// 群聊自定义消息发送成功} fail:^(int code, NSString *desc) {// 群聊自定义消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建自定义消息V2TIMString str = u8"群聊自定义消息";V2TIMBuffer customData = {reinterpret_cast<const uint8_t*>(str.CString()), str.Size()};V2TIMMessage v2TIMMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateCustomMessage(customData);// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 发送群聊自定义消息成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 发送群聊自定义消息失败delete callback;},[=](uint32_t progress) {// 自定义消息不会回调进度});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, {}, "receiver_groupID", V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
createXxxMessage
创建指定类型的富媒体消息对象,其中 Xxx 表示具体的消息类型。// 创建图片消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createImageMessage("/sdcard/xxx");// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 图片上传进度,progress 取值 [0, 100]}@Overridepublic void onSuccess(V2TIMMessage message) {// 图片消息发送成功}@Overridepublic void onError(int code, String desc) {// 图片消息发送失败}});
// 获取本地图片路径NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];// 创建图片消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createImageMessage:imagePath];// 发送消息[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_DEFAULTonlineUserOnly:NOofflinePushInfo:nilprogress:^(uint32_t progress) {// 图片上传进度,progress 取值 [0, 100]} succ:^{// 图片消息发送成功} fail:^(int code, NSString *desc) {// 图片消息发送成功}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建图片消息V2TIMMessage v2TIMMessage =V2TIMManager::GetInstance()->GetMessageManager()->CreateImageMessage("./File/Xxx.jpg");// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 图片消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 图片消息发送失败delete callback;},[=](uint32_t progress) {// 图片上传进度,progress 取值 [0, 100]});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
// 创建语音消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createSoundMessage("/sdcard/xxx", 5);// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 语音上传进度,progress 取值 [0, 100]}@Overridepublic void onSuccess(V2TIMMessage message) {// 语音消息发送成功}@Overridepublic void onError(int code, String desc) {// 语音消息发送失败}});
// 获取本地语音文件路径NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4a"];// 获取语音素材的时长(时长仅用于 UI 显示)AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:soundPath]];CMTime time = [asset duration];int duration = ceil(time.value/time.timescale);// 创建语音消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createSoundMessage:soundPath duration:duration];// 发送消息[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_DEFAULTonlineUserOnly:NOofflinePushInfo:nilprogress:^(uint32_t progress) {// 语音上传进度,progress 取值 [0, 100]} succ:^{// 语音消息发送成功} fail:^(int code, NSString *desc) {// 语音消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建语音消息V2TIMMessage v2TIMMessage =V2TIMManager::GetInstance()->GetMessageManager()->CreateSoundMessage("./File/Xxx.mp3", 5);// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 语音消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 语音消息发送失败delete callback;},[=](uint32_t progress) {// 语音上传进度,progress 取值 [0, 100]});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
// 创建视频消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createVideoMessage("/sdcard/xxx", "mp4", 10, "/sdcard/xxx");// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 视频上传进度,progress 取值 [0, 100]}@Overridepublic void onSuccess(V2TIMMessage message) {// 视频消息发送成功}@Overridepublic void onError(int code, String desc) {// 视频消息发送失败}});
// 获取本地视频文件路径NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];// 获取本地视频快照路径NSString *snapShotPath = [[NSBundle mainBundle] pathForResource:@"testpng" ofType:@""];// 获取视频时长AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];CMTime time = [asset duration];int duration = ceil(time.value/time.timescale);// 创建视频消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createVideoMessage:videoPathtype:@"mp4"duration:durationsnapshotPath:snapShotPath];// 发送消息[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_DEFAULTonlineUserOnly:NOofflinePushInfo:nilprogress:^(uint32_t progress) {// 视频上传进度,progress 取值 [0, 100]} succ:^{// 视频消息发送成功} fail:^(int code, NSString *desc) {// 视频消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建视频消息V2TIMMessage v2TIMMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateVideoMessage("./File/Xxx.mp4", "mp4", 10, "./File/Xxx.jpg");// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 视频消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 视频消息发送失败delete callback;},[=](uint32_t progress) {// 视频上传进度,progress 取值 [0, 100]});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
// 创建文件消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createFileMessage("/sdcard/xxx", "文件名");// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 文件上传进度,progress 取值 [0, 100]}@Overridepublic void onSuccess(V2TIMMessage message) {// 文件消息发送成功}@Overridepublic void onError(int code, String desc) {// 文件消息发送失败}});
// 获取本地文件路径NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];// 创建文件消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createFileMessage:filePath fileName:@"发送文件消息"];// 发送消息[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_DEFAULTonlineUserOnly:NOofflinePushInfo:nilprogress:^(uint32_t progress) {// 文件上传进度,progress 取值 [0, 100]} succ:^{// 文件消息发送成功} fail:^(int code, NSString *desc) {// 文件消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建文件消息V2TIMMessage v2TIMMessage =V2TIMManager::GetInstance()->GetMessageManager()->CreateFileMessage("./File/Xxx.zip", "文件名");// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 文件消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 文件消息发送失败delete callback;},[=](uint32_t progress) {// 文件上传进度,progress 取值 [0, 100]});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
// 创建定位消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createLocationMessage("地理位置", 0.5, 0.5);// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 定位消息不回调进度}@Overridepublic void onSuccess(V2TIMMessage message) {// 定位消息发送成功}@Overridepublic void onError(int code, String desc) {// 定位消息发送失败}});
// 创建定位消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createLocationMessage:@"发送地理位置消息" longitude:0.5 latitude:0.5];// 发送消息[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_DEFAULTonlineUserOnly:NOofflinePushInfo:nilprogress:nilsucc:^{// 定位消息发送成功} fail:^(int code, NSString *desc) {// 定位消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建定位消息V2TIMMessage v2TIMMessage =V2TIMManager::GetInstance()->GetMessageManager()->CreateLocationMessage("地理位置", 0.5, 0.5);// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 定位消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 定位消息发送失败delete callback;},[=](uint32_t progress) {// 定位消息不回调进度});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
// 创建表情消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createFaceMessage(1, "tt00".getBytes());// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 表情消息不回调进度}@Overridepublic void onSuccess(V2TIMMessage message) {// 表情消息发送成功}@Overridepublic void onError(int code, String desc) {// 表情消息发送失败}});
// 创建表情消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createFaceMessage:1 data:[@"tt00" dataUsingEncoding:NSUTF8StringEncoding]];// 发送消息[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_DEFAULTonlineUserOnly:NOofflinePushInfo:nilprogress:nilsucc:^{// 表情消息发送成功} fail:^(int code, NSString *desc) {// 表情消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建表情消息V2TIMString str = u8"tt00";V2TIMBuffer data = {reinterpret_cast<const uint8_t*>(str.CString()), str.Size()};V2TIMMessage v2TIMMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateFaceMessage(1, data);// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 表情消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 表情消息发送失败delete callback;},[=](uint32_t progress) {// 表情消息不回调进度});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
appendElem
(Android / iOS & Mac / Windows) 方法添加下一个 elem 成员。appendElem
仅支持在原有的 V2TIMElem
(此 Elem 类型不限)后面追加 V2TIMTextElem
、V2TIMCustomElem
、V2TIMFaceElem
和 V2TIMLocationElem
四种类型的元素。
因此 “图片 + 文本”、“视频 + 文本”、“位置 + 文本” 这几种形式均可支持,但是 “图片 + 图片”、“文本 + 图片” 等不支持。// 创建文本消息V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createTextMessage("test");// 创建自定义 elemV2TIMCustomElem customElem = new V2TIMCustomElem();customElem.setData("自定义消息".getBytes());// 将自定义 elem 添加到 message.textElem 中v2TIMMessage.getTextElem().appendElem(customElem);// 发送消息V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, "receiver_userID", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {// 不回调进度}@Overridepublic void onSuccess(V2TIMMessage message) {// 多 elem 消息发送成功}@Overridepublic void onError(int code, String desc) {// 多 elem 消息发送失败}});
// 创建文本消息V2TIMMessage *message = [[V2TIMManager sharedInstance] createTextMessage:@"text"];// 创建自定义 elemV2TIMCustomElem *customElem = [[V2TIMCustomElem alloc] init];customElem.data = [@"自定义消息" dataUsingEncoding:NSUTF8StringEncoding];// 将自定义 elem 添加到 message.textElem 中[message.textElem appendElem:customElem];// 发送消息[[V2TIMManager sharedInstance] sendMessage:messagereceiver:@"userID"groupID:nilpriority:V2TIM_PRIORITY_DEFAULTonlineUserOnly:NOofflinePushInfo:nilprogress:nilsucc:^{// 多 elem 消息发送成功} fail:^(int code, NSString *desc) {// 多 elem 消息发送失败}];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};// 创建文本消息V2TIMMessage v2TIMMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage("test");// 创建自定义 elemV2TIMCustomElem customElem;V2TIMString str = u8"tt00";customElem.data = {reinterpret_cast<const uint8_t*>(str.CString()), str.Size()};// 将自定义 elem 添加到 message.elemList 中v2TIMMessage.elemList.PushBack(&customElem);// 发送消息auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 多 elem 消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 多 elem 消息发送失败delete callback;},[=](uint32_t progress) {// 不回调进度});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(v2TIMMessage, "receiver_userID", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
功能特性 | 限制项 | 限制说明 |
单聊/群聊 | 内容长度 | 单聊、群聊消息,单条消息最大长度限制为 12K。 |
| 发送频率 | 单聊消息:客户端发送单聊消息无限制;REST API 发送有频率限制,可查看相应接口的文档。 群聊消息:每个群限 40 条/秒(针对所有群类型、所有平台接口)。不同群内发消息,限频互不影响。 |
| 接收频率 | 单聊和群聊均无限制。 |
| 单个文件大小 | 发送文件消息时,SDK 最大支持发送单个文件大小为 100MB。 |
本页内容是否解决了您的问题?