メッセージの取り消し
Overview
The sender can recall a successfully sent message.
By default, the sender can recall a message that is sent within 2 minutes. You can change the time limit for message recall. For detailed directions, see Message recall settings.
Message recall can be implemented through the receiver UI code: When a message is recalled, the receiver will receive the
onRecvMessageRevoked
notification which contains the msgID
of the recalled message. You can identify the recalled message at the UI layer based on the msgID
and change the bubble for the message to the "Message recalled" status.In one-to-one chats, only the sender can recall their own messages through this API.
In group chats, not only the sender can recall their own message, but administrators or group owners can also recall messages sent by other group members through this API.
This API is available for all group types, including audio-video groups (AVChatRoom) and community groups, in 7.4 and later versions.
Recalling a Message (by the Sender)
Sample code:
V2TIMManager.getMessageManager().revokeMessage(v2TIMMessage, new V2TIMCallback() {@Overridepublic void onError(int code, String desc) {// The message failed to be recalled}@Overridepublic void onSuccess() {// Message recalled successfully}});
// `selectedMessage` is the message to be recalled.[[V2TIMManager sharedInstance] revokeMessage:selectedMessagesucc:^{// Message recalled successfully} fail:^(int code, NSString *msg) {// The message failed to be recalled}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new Callback;callback->SetCallback([=]() {// Message recalled successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// The message failed to be recalleddelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->RevokeMessage(message, callback);
Noticing a Message Recall (by the Receiver)
1. The receiver calls
addAdvancedMsgListener
(Android / iOS and macOS / Windows) to set the advanced message listener.2. The receiver receives the message recall notification in
onRecvMessageRevoked
(Android / iOS & Mac / Windows) .Note:
Starting from version 7.4, when a message is revoked, the receiver can obtain the reason for revocation and the revoker of the message through the API onRecvMessageRevoked.
Sample code:
V2TIMManager.getMessageManager().addAdvancedMsgListener(new V2TIMAdvancedMsgListener() {@Overridefor (V2TIMMessage msg : msgList) {if (msg.getMsgID().equals(msgID)) {// You need to change the bubble status for the message on the UI after receiving the message recall notification.}}}}
// V2TIMAdvancedMsgListener- (void)onRecvMessageRevoked:(NSString *)msgID operateUser:(V2TIMUserFullInfo *)operateUser reason:(NSString *)reason {// You need to change the bubble status for the message on the UI after receiving the message recall notification.}
class AdvancedMsgListener final : public V2TIMAdvancedMsgListener {public:/*** Received a new message** @param message Message*//*** Received a message recall notification** @param messageID Unique message ID*/void OnRecvMessageRevoked(const V2TIMString& messageID,// `msgList` is the message list on the current chat interface}// Other members …};// Add an event listener for advanced messages. Keep `advancedMsgListener` valid before it is removed to ensure event callbacks are received.AdvancedMsgListener advancedMsgListener;V2TIMManager::GetInstance()->GetMessageManager()->AddAdvancedMsgListener(&advancedMsgListener);