createXxxMessage
(其中 Xxx 表示具体的消息类型) 接口创建一条原始消息对象 V2TIMMessage
。createTargetedGroupMessage
(Android / iOS & Mac / Windows) 接口根据原始消息对象创建定向消息对象 V2TIMMessage
,并指定消息接收成员列表。sendMessage
接口发送定向消息。// 创建原始消息对象V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createTextMessage("这是一个群定向消息");// 创建定向群消息对象,指定群消息接收者 "vinson" "denny"List<String> targetGroupMemberList = new ArrayList<>();targetGroupMemberList.add("vinson");targetGroupMemberList.add("denny");V2TIMMessage targetGroupMessage = V2TIMManager.getMessageManager().createTargetedGroupMessage(v2TIMMessage, targetGroupMemberList);// 在群 "groupA" 发送群定向消息V2TIMManager.getMessageManager().sendMessage(targetGroupMessage, null, "groupA", V2TIMMessage.V2TIM_PRIORITY_DEFAULT, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onError(int code, String desc) {// 发送失败}@Overridepublic void onSuccess(V2TIMMessage v2TIMMessage) {// 发送成功}@Overridepublic void onProgress(int progress) {}});
// 创建原始消息对象V2TIMMessage *message = [V2TIMManager.sharedInstance createTextMessage:@"这是一个群定向消息"];// 创建定向群消息对象,指定群消息接收者 "vinson" "denny"NSMutableArray *receiverList = [NSMutableArray array];[receiverList addObject:@"vinson"];[receiverList addObject:@"denny"];V2TIMMessage *targetGroupMessage = [V2TIMManager.sharedInstance createTargetedGroupMessage:message receiverList:receiverList];// 在群 "groupA" 发送定向消息[[V2TIMManager sharedInstance] sendMessage:targetGroupMessage receiver:nil groupID:@"groupA"priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {} 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_;};// 创建原始消息对象V2TIMMessage message =V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage(u8"这是一条群 @ 文本消息");// 创建定向群消息对象,指定群消息接收者 "vinson" "denny"V2TIMStringVector receiverList;receiverList.PushBack("vinson");receiverList.PushBack("denny");V2TIMMessage targetGroupMessage =V2TIMManager::GetInstance()->GetMessageManager()->CreateTargetedGroupMessage(message, receiverList);auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) {// 群定向消息发送成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 群定向消息发送失败delete callback;},[=](uint32_t progress) {});// 在群 "groupA" 发送群定向消息V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(targetGroupMessage, {}, "groupA", V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);
本页内容是否解决了您的问题?