please select

Modify a Message

Feature Description

This feature enables any participant in a conversation to modify a successfully sent message in the conversation. The message will be synced to all the participants in the conversation once modified successfully.
Note:
This feature is supported only by the Enhanced edition on v6.2 or later.

Effect

You can use this API to change the cloudCustomData of a message, implementing features such as message reply and message quote as shown below:




API Description

Modifying a Message

A conversation participant can call modifyMessage (Android / iOS and macOSWindows) to modify a sent message in the conversation. The SDK allows any conversation participant to modify a message in the conversation. You can add more restrictions at the business layer, for example, only allowing the message sender to modify the message.
Currently, the following information of a message can be modified:
cloudCustomData (Android / iOS and macOSWindows)
V2TIMTextElem (Android / iOS and macOSWindows)
V2TIMCustomElem (Android / iOS and macOSWindows)
V2TIMLocationElem (Android / iOS and macOSWindows)
V2TIMFaceElem (Android / iOS and macOSWindows)
Sample code:
Android
iOS and macOS
Windows
// The original message object in the conversation is `originMessage`.
// Modify the `cloudCustomData` information of the message object
originMessage.setCloudCustomData("modify_cloud_custom_data".getBytes());
// If the message is a text message, modify the text message content
if (V2TIMMessage.V2TIM_ELEM_TYPE_TEXT == originMessage.getElemType()) {
originMessage.getTextElem().setText("modify_text");
}
V2TIMManager.getMessageManager().modifyMessage(originMessage, new V2TIMCompleteCallback<V2TIMMessage>() {
@Override
public void onComplete(int code, String desc, V2TIMMessage message) {
// After the message is modified, `message` is the modified message object.
}
});
// Original message object in the conversation
V2TIMMessage *originMessage;
// Modify the `cloudCustomData` information of the message object
originMessage.cloudCustomData = [@"modify_cloud_custom_data" dataUsingEncoding:NSUTF8StringEncoding];
// If the message is a text message, modify the text message content
if (V2TIM_ELEM_TYPE_TEXT == originMessage.elemType) {
originMessage.textElem.text = @"modify_text";
}
[[V2TIMManager sharedInstance] modifyMessage:originMessage completion:^(int code, NSString *desc, V2TIMMessage *msg) {
// After the message is modified, `msg` is the modified message object.
}];
template <class T>
class CompleteCallback final : public V2TIMCompleteCallback<T> {
public:
using InternalCompleteCallback =
std::function<void(int, const V2TIMString&, const T&)>;

CompleteCallback() = default;
~CompleteCallback() override = default;

void SetCallback(InternalCompleteCallback complete_callback) { complete_callback_ = std::move(complete_callback); }

void OnComplete(int error_code, const V2TIMString& error_message, const T& value) override {
if (complete_callback_) {
complete_callback_(error_code, error_message, value);
}
}

private:
InternalCompleteCallback complete_callback_;
};

// V2TIMMessage originMessage;
std::string str = u8"modify_cloud_custom_data";
// Modify the `cloudCustomData` information of the message object
originMessage.cloudCustomData = {reinterpret_cast<const uint8_t*>(str.data()), str.size()};
if (originMessage.elemList.Size() == 1) {
V2TIMElem* elem = originMessage.elemList[0];
if (elem->elemType == V2TIMElemType::V2TIM_ELEM_TYPE_TEXT) {
// If the message is a text message, modify the text message content
auto textElem = static_cast<V2TIMTextElem*>(elem);
textElem->text = "modify_text";
}
}

auto callback = new CompleteCallback<V2TIMMessage>{};
callback->SetCallback([=](int error_code, const V2TIMString& error_message, const V2TIMMessage& message) {
// After the message is modified, `message` is the modified message object.
delete callback;
});

V2TIMManager::GetInstance()->GetMessageManager()->ModifyMessage(originMessage, callback);

Message Modified Notification

Conversation participants call addAdvancedMsgListener (Android / iOS and macOSWindows) to add the advanced message listener.
After a message in the conversation is modified, all the participants will receive the onRecvMessageModified callback (Android / iOS and macOSWindows), which contains the modified message object.
Sample code:
Android
iOS and macOS
Windows
V2TIMAdvancedMsgListener advancedMsgListener = new V2TIMAdvancedMsgListener() {
// Notification of the message content modification
@Override
public void onRecvMessageModified(V2TIMMessage msg) {
// `msg` is the modified message object.
}
};
// Add a message listener
V2TIMManager.getMessageManager().addAdvancedMsgListener(advancedMsgListener);
// Add a message listener
[[V2TIMManager sharedInstance] addAdvancedMsgListener:self];
/// Notification of the message content modification
- (void)onRecvMessageModified:(V2TIMMessage *)msg {
// `msg` is the modified message object.
}
class AdvancedMsgListener final : public V2TIMAdvancedMsgListener {
public:
// Notification of the message content modification
void OnRecvNewMessage(const V2TIMMessage& message) override {
// `message` is the modified message object.
}
// Other members ...
};

// Note that `advancedMsgListener` should not be released before the IM SDK is uninitialized,
// otherwise the message callback cannot be called.
AdvancedMsgListener advancedMsgListener;
V2TIMManager::GetInstance()->GetMessageManager()->AddAdvancedMsgListener(&advancedMsgListener);