Forward Messages

Feature Description

You can combine and forward messages in the following steps:
1. Create a combined message based on the list of original messages.
2. Send the combined message to the receiver.
3. The receiver receives the combined message and parses the list of original messages.
The title and digest are needed to display the combined message, as shown below:
Combine and Forward
Display of Combined Message
Click Combined Message to Download Message List for Display










Combining and Forwarding Messages

Creating and sending a combined message

A combined message can be created by setting the message list along with the combined message title and digest. The process is as follows:
1. Call the createMergerMessage API (Android / iOS and macOSWindows)  to create a combined message. The list of original messages as well as the combined message title and digest also need to be set.



Attribute
Description
Remarks
messageList
List of original messages
List of original messages to be combined and forwarded
title
Title
Title of the combined message, such as "Chat History of xixiyah and Hello" as shown above
abstractList
Digest list
Digest list of the combined message as shown above. The original message digests need to be displayed for the combined message, which will be unfolded after the user clicks the cell.
compatibleText
Compatibility text message
If the early SDK version does not support the combined message, the user will receive a text message with the content compatibleText by default.
Sample code:
Android
iOS and macOS
Windows
// List of messages to be forwarded, which can contain combined messages but not group tips
List<V2TIMMessage> msgs = new ArrayList<>();
msgs.add(message1);
msgs.add(message2);
// Title of the combined message
String title = "Chat History of vinson and lynx";
// Digest list of the combined message
List<String> abstactList = new ArrayList<>();
msgs.add("abstract1");
msgs.add("abstract2");
msgs.add("abstract3");
// Compatibility text of the combined message. If the early SDK version does not support the combined message, the user will receive a text message with the content `compatibleText` by default.
String compatibleText = "Upgrade to the latest version to check the merged message";
// Create a combined message
V2TIMMessage mergeMessage = V2TIMManager.getMessageManager().createMergerMessage(msgs, title, abstractList, compatibleText);
// List of messages to be forwarded, which can contain combined messages but not group tips
NSArray *msgs = @[message1,message2...];
// Title of the combined message
NSString *title = @"Chat History of vinson and lynx";
// Digest list of the combined message
NSArray *abstactList = @[@"abstract1",@"abstract2",@"abstract3"];
// Compatibility text of the combined message. If the SDK on an early version does not support the combined message, the user will receive a text message with the content `compatibleText` by default.
NSString *compatibleText = @"Upgrade to the latest version to check the merged message";
// Create a combined message
V2TIMMessage *mergeMessage = [[V2TIMManager sharedInstance] createMergerMessage:msgs title:title
abstractList:abstactList compatibleText:compatibleText];
// List of messages to be forwarded, which can contain combined messages but not group tips
V2TIMMessageVector messageList;
messageList.PushBack(message1);
messageList.PushBack(message2);
// Title of the combined message
V2TIMString title = "Chat History of vinson and lynx";
// Digest list of the combined message
V2TIMStringVector abstractList;
abstractList.PushBack("abstract1");
abstractList.PushBack("abstract2");
abstractList.PushBack("abstract3");
// Compatibility text of the combined message. If the early SDK version does not support the combined message,
// the user will receive a text message with the content `compatibleText` by default.
V2TIMString compatibleText = "Upgrade to the latest version to check the merged message";
// Create a combined message
V2TIMMessage mergerMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateMergerMessage(
messageList, title, abstractList, compatibleText);
2. Call the sendMessage API (Android / iOS and macOSWindows) to send the combined message.
Sample code:
Android
iOS and macOS
Windows
// Send the combined message to the user `denny`
V2TIMManager.getMessageManager().sendMessage(mergeMessage, "denny", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {
@Override
public void onProgress(int progress) {}

@Override
public void onSuccess(V2TIMMessage v2TIMMessage) {}

@Override
public void onError(int code, String desc) {}
})
// Send the combined message to the user `denny`
[[V2TIMManager sharedInstance] sendMessage:mergeMessage receiver:@"denny" groupID:nil
priority:V2TIM_PRIORITY_NORMAL onlineUserOnly:NO offlinePushInfo:nil progress:nil succ:nil fail:nil];
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) {});

V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(
mergerMessage, "denny", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);

Receiving a combined message

Adding a listener

The receiver can call addAdvancedMsgListener (Android / iOS and macOSWindows) to add the advanced message listener. We recommend it be called early, such as after the chat page is initialized, to ensure timely message receiving in the application.
Sample code:
Android
iOS and macOS
Windows
V2TIMManager.getMessageManager().addAdvancedMsgListener(advancedMsgListener);
// `self` is id<V2TIMAdvancedMsgListener>
[[V2TIMManager sharedInstance] addAdvancedMsgListener:self];
class AdvancedMsgListener final : public V2TIMAdvancedMsgListener {
// 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);

Parsing a message

After the listener is added, the receiver will receive the combined message V2TIMMessage in onRecvNewMessage. You can use the combined message element V2TIMMergerElem (Android / iOS and macOSWindows) to get the title and abstractList for UI display. Then, when the user clicks the combined message, you can call the downloadMergerMessage API (Android / iOS and macOSWindows) to download the combined message list for UI display.
Sample code:
Android
iOS and macOS
Windows
@Override
public void onRecvNewMessage(V2TIMMessage msg) {
if (msg.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_MERGER) {
// Get the elements of the combined message
V2TIMMergerElem mergerElem = msg.getMergerElem();
// Get the `title`
String title = mergerElem.getTitle();
// Get the digest list
List<String> abstractList = mergerElem.getAbstractList();
// Download the combined message list when the user clicks the combined message
mergerElem.downloadMergerMessage(new V2TIMValueCallback<List<V2TIMMessage>>() {
@Override
public void onSuccess(List<V2TIMMessage> v2TIMMessages) {
// Downloaded successfully. `v2TIMMessages` is the combined message list.
for (V2TIMMessage subMsg : v2TIMMessages) {
// If the combined message list still contains a combined message, continue to parse the message.
if (subMsg.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_MERGER) {
V2TIMMergerElem mergerElem = subMsg.getMergerElem();
// Get the `title`
String title = mergerElem.getTitle();
// Get the digest list
List<String> abstractList = mergerElem.getAbstractList();
// Download the combined message list when the user clicks the combined message
......
}
}
}

@Override
public void onError(int code, String desc) {
// Download failed
}
});
}
- (void)onRecvNewMessage:(V2TIMMessage *)msg {
if (msg.elemType == V2TIM_ELEM_TYPE_MERGER) {
// Get the elements of the combined message
V2TIMMergerElem *mergerElem = msg.mergerElem;
// Get the `title`
NSString *title = mergerElem.title;
// Get the digest list
NSArray *abstractList = mergerElem.abstractList;
// Download the combined message list when the user clicks the combined message
[msg.mergerElem downloadMergerMessage:^(NSArray<V2TIMMessage *> *msgs) {
// Downloaded successfully. `msgs` is the combined message list.
for (V2TIMMessage *subMsg in msgs) {
// If the combined message list still contains a combined message, continue to parse the message.
if (subMsg.elemType == V2TIM_ELEM_TYPE_MERGER) {
V2TIMMergerElem *mergerElem = subMsg.mergerElem;
// Get the `title`
NSString *title = mergerElem.title;
// Get the digest list
NSArray *abstractList = mergerElem.abstractList;
// Download the combined message list when the user clicks the combined message
[msg.mergerElem downloadMergerMessage:nil fail:nil];
}
}
} fail:^(int code, NSString *desc) {
// Download failed
}];
}
}
template <class T>
class ValueCallback final : public V2TIMValueCallback<T> {
public:
using SuccessCallback = std::function<void(const T&)>;
using ErrorCallback = std::function<void(int, const V2TIMString&)>;

ValueCallback() = default;
~ValueCallback() override = default;

void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
success_callback_ = std::move(success_callback);
error_callback_ = std::move(error_callback);
}

void OnSuccess(const T& value) override {
if (success_callback_) {
success_callback_(value);
}
}
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_;
};

class AdvancedMsgListener final : public V2TIMAdvancedMsgListener {
public:
void OnRecvNewMessage(const V2TIMMessage& message) override {
if (message.elemList.Size() == 1) {
V2TIMElem* elem = message.elemList[0];
if (elem->elemType == V2TIMElemType::V2TIM_ELEM_TYPE_MERGER) {
// Get the elements of the combined message
auto mergerElem = static_cast<V2TIMMergerElem*>(elem);
// Get the `title`
V2TIMString title = mergerElem->title;
/// Get the digest list
V2TIMStringVector abstractList = mergerElem->abstractList;

// Download the combined message list when the user clicks the combined message
auto callback = new ValueCallback<V2TIMMessageVector>{};
callback->SetCallback(
[=](const V2TIMMessageVector& messageList) {
// Downloaded successfully. `v2TIMMessages` is the combined message list.
for (size_t i = 0; i < messageList.Size(); ++i) {
const V2TIMMessage& message = messageList[i];
if (message.elemList.Size() == 1) {
V2TIMElem* elem = message.elemList[0];
// If the combined message list still contains a combined message, continue to parse the message.
if (elem->elemType == V2TIMElemType::V2TIM_ELEM_TYPE_MERGER) {
// ...
}
}
}
delete callback;
},
[=](int error_code, const V2TIMString& error_message) { delete callback; });

mergerElem->DownloadMergerMessage(callback);
}
}
}
// 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);

Removing a listener

To stop receiving messages, the receiver can call removeAdvancedMsgListener (Android / iOS and macOSWindows) to remove the advanced message listener.
Sample code:
Android
iOS and macOS
Windows
V2TIMManager.getMessageManager().removeAdvancedMsgListener(advancedMsgListener);
// `self` is id<V2TIMAdvancedMsgListener>
[[V2TIMManager sharedInstance] removeAdvancedMsgListener:self];
class AdvancedMsgListener final : public V2TIMAdvancedMsgListener {
// Members ...
};

// `advancedMsgListener` is the instance of AdvancedMsgListener
V2TIMManager::GetInstance()->GetMessageManager()->RemoveAdvancedMsgListener(&advancedMsgListener);

Forwarding Messages One by One

To forward a single message, create a message identical to the original message through the createForwardMessage API (Android / iOS and macOSWindows) first, and then call the sendMessage API (Android / iOS and macOSWindows) to send the message.
Sample code:
Android
iOS and macOS
Windows
// Create a message with the same elements as the original message
V2TIMMessage forwardMessage = V2TIMManager.getMessageManager().createForwardMessage(originMsg);
// Send the message to the user `denny`
V2TIMManager.getMessageManager().sendMessage(forwardMessage, "denny", null, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {
@Override
public void onProgress(int progress) {
}

@Override
public void onSuccess(V2TIMMessage message) {
}

@Override
public void onError(int code, String desc) {
}
});
// Create a message with the same elements as the original message
V2TIMMessage *forwardMessage = [[V2TIMManager sharedInstance] createForwardMessage:originMsg];
// Send the message to the user `denny`
[[V2TIMManager sharedInstance] sendMessage:forwardMessage receiver:@"denny" groupID:nil
priority:V2TIM_PRIORITY_NORMAL onlineUserOnly:NO offlinePushInfo:nil progress:nil succ:nil fail:nil];
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_;
};

// Create a message with the same elements as the original message
V2TIMMessage forwardMessage = V2TIMManager::GetInstance()->GetMessageManager()->CreateForwardMessage(originMsg);

auto callback = new SendCallback{};
callback->SetCallback([=](const V2TIMMessage& message) { delete callback; },
[=](int error_code, const V2TIMString& error_message) { delete callback; },
[=](uint32_t progress) {});

// Send the message to the user `denny`
V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(
forwardMessage, "denny", {}, V2TIMMessagePriority::V2TIM_PRIORITY_NORMAL, false, {}, callback);