logo
On this page

Set message extension field


Introduction

ZEGOCLOUD Instant Messaging (ZIM) SDK allows you to add an extension field to a message and send the extension field as additional message information. Message extension fields can be visible to the peer end or visible only to the local end based on the synchronization effects. A message extension field is used to present information such as the translation status and translation content of a message, and the business logic included in the message.

Implementation Steps

  1. Create a ZIM instance.
  2. Log in to the ZIM SDK.
  3. Construct the ZIMMessage object, and fill in the ZIMMessage field in the extendedData object as the message extension field.
  4. Call the sendMessage interface, pass in the ZIMMessage object, send the message and expand the fields.
Sample code
zim::ZIMMessage* message = nullptr;
zim::ZIMTextMessage text_message;
text_message.message = "message";
// The extended fields that need to be sent
text_message.extendedData = "extendedData";
// Set message priority
zim::ZIMMessageSendConfig config;
config.priority = zim::ZIM_MESSAGE_PRIORITY_LOW;
message = &text_message;

// Set the conversation type
zim::ZIMConversationType type = zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER

auto notification = std::make_shared<zim::ZIMMessageSendNotification>(
            [=](const std::shared_ptr<zim::ZIMMessage> &message) { int i = 0; });

zim_->sendMessage(message, "toConversationID", type, config, notification,
                    [=](const std::shared_ptr<zim::ZIMMessage> &message,
                              const zim::ZIMError &errorInfo) { int i = 0; });
1
Copied!

Configure an extension field visible only to the local end

Procedure for sending a message:

  1. Log in to ZIM SDK, construct a ZIMMessagefield as an extension field visible only to the local end in the ZIMMessage object.
  2. Call the sendMessage operation and pass in the ZIMMessage object to send the message and the extension field.
Sample code

  // 1. Configure the local extension field of a message.

  // Specify the conversation ID.
  std::string conversationID = "conv";

  // Construct a ZIMMessage object.
  auto zimMessage = std::make_shared<zim::ZIMTextMessage>(message: 'message');

  // Configure the local extension field of a message.
  zimMessage->localExtendedData =  "Local extension field of the message";

  auto sendConfig = zim::ZIMMessageSendConfig();
  // Configure the priority of the message.
  sendConfig.priority = zim::ZIMMessagePriority::ZIM_MESSAGE_PRIORITY_LOW;
  
  //Set the conversation type sent
  // Send a private-chat message.
  auto conversationType = zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER;

  // Send a group-chat message.
  // auto conversationType = zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_GROUP;


  // Send a room message.
  // auto conversationType = zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_ROOM;

  // 2. Send the message.
 zim_->sendMessage(zimMessage, conversationID, conversationType, sendConfig,
                    std::make_shared<zim::ZIMMessageSendNotification>(
                        [=](const std::shared_ptr<zim::ZIMMessage> &message) {
                              
                        }),
                    [=](const std::shared_ptr<zim::ZIMMessage> &message,
                        const zim::ZIMError &errorInfo) {
                              
                        });
1
Copied!

Update the local extension field of a message

For a message that is already received or sent, you can call the updateMessageLocalExtendedData operation to update the local extension field of the message.

The following sample code shows how to update the local extension field of a message that is already received:

Sample code
// Update the local extension field of a private-chat message after you receive the message.
void onPeerMessageReceived(zim::ZIM *zim,
                              const std::vector<std::shared_ptr<zim::ZIMMessage>> &messageList,
                              const ZIMMessageReceivedInfo info,
                              const std::string &fromUserID) override {
    for (const auto &message : messageList) {
        zim->updateMessageLocalExtendedData("Update the local extension field.", message,
                                            [=](const std::shared_ptr<zim::ZIMMessage> &msg,
                                                const zim::ZIMError &errorInfo) {
                                                    // Developers can use this callback to monitor whether the local extension field is updated successfully.                                                });
    }
}
1
Copied!

Previous

Read receipts

Next

Search for local messages