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
// 1. Create a ZIM object and pass in the appID and appSign
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345;  // Replace with the AppID you obtained from ZEGO Console
appConfig.appSign = "appSign";   // Replace with the AppSign you obtained from ZEGO Console
ZIM.Create(appConfig);

// 2. Log in
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = "xxxx";
zimUserInfo.userName = "xxxx";
ZIM.GetInstance().Login(zimUserInfo, (ZIMError errorInfo) =>
    {
        // Developers can determine whether the login was successful based on ZIMError.
    }
);

// 3. Configure message extension fields
string toConversationID = "xxxx1";

ZIMTextMessage zimMessage = new ZIMTextMessage();
zimMessage.message = "Message content";
// Extension fields to be sent
zimMessage.extendedData = "Message extension fields";

ZIMMessageSendConfig config = new ZIMMessageSendConfig();
// Set message priority
config.priority = ZIMMessagePriority.Low;

// 4. Set the conversation type for sending
// Send one-on-one chat messages
ZIMConversationType type = ZIMConversationType.Peer;

// Send group chat messages
ZIMConversationType groupType = ZIMConversationType.Group;

// Send room chat messages
ZIMConversationType roomType = ZIMConversationType.Room;

// 5. Send messages
ZIMMessageSendNotification notification = new ZIMMessageSendNotification();
ZIM.GetInstance().SendMessage(zimMessage, toConversationID, type, config, notification, 
    (ZIMMessage message, ZIMError errorInfo) => 
{
    // Callback result of message sending
});
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 session ID.
  String toConversationID = "xxx1";

  // Construct a ZIMMessage object.
  ZIMTextMessage zimMessage = new ZIMTextMessage();
  zimMessage.message = "message";

  // Configure the local extension field of a message.

  zimMessage.localExtendedData = "Local extension field of the message";

  ZIMMessageSendConfig config = new ZIMMessageSendConfig();

  // Configure the priority of the message.
  config.priority = ZIMMessagePriority.Low;

  // Set the session type sent
  // Send a private-chat message.
  ZIMConversationType peerType = ZIMConversationType.Peer;

  // Send a group-chat message.
  ZIMConversationType groupType = ZIMConversationType.Group;

  // Send a room message.
  ZIMConversationType roomType = ZIMConversationType.Room;

  // 2. Send the message.
  ZIM.GetInstance().SendMessage(zimMessage, toConversationID, peerType, config, notification, (ZIMMessage message, ZIMError errorInfo) =>
{
    // Developers can use this callback to monitor whether the message is sent successfully.
});


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
ZIM.GetInstance().onReceivePeerMessage += (ZIM zim,
                                List<ZIMMessage> messageList,
                                string fromUserID) =>
    {
        foreach (ZIMMessage message in messageList)
        {
            ZIM.GetInstance().UpdateMessageLocalExtendedData("Update the local extension field",
                message,
                (ZIMMessage message, 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