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
- Create a ZIM instance.
- Log in to the ZIM SDK.
- Construct the ZIMMessage object, and fill in the ZIMMessage field in the extendedData object as the message extension field.
- Call the sendMessage interface, pass in the ZIMMessage object, send the message and expand the fields.
// 1. Create a ZIM object, pass in appID, appSign and Application in Android
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345; // Replace it with the AppID you applied for from the ZEGO Admin Console
appConfig.appSign = "appSign"; // Replace with the AppSign you applied for from the ZEGO Admin Console
zim = ZIM.create(appConfig, application);
// 2. Set the setEventHandler callback
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onReceivePeerMessage(ZIM zim, ArrayList<ZIMMessage> messageList, String fromUserID) {
// Callback for receiving a single chat message
}
});
// 3. Login
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = "xxxx";
zimUserInfo.userName = "xxxx";
zim.login(zimUserInfo, new ZIMLoggedInCallback() {
@Override
public void onLoggedIn(ZIMError error) {
// Developers can judge whether the login is successful according to ZIMError.
}
});
String toConversationID = "xxxx1";
ZIMTextMessage zimMessage = new ZIMTextMessage();
zimMessage.message = "Message content";
// The extended fields that need to be sent
zimMessage.extendedData = "Message Extension Field";
ZIMMessageSendConfig config = new ZIMMessageSendConfig();
// Set message priority
config.priority = ZIMMessagePriority.LOW;
// 4. Set the session type to send
// Send one-on-one chat information
ZIMConversationType type = ZIMConversationType.Peer;
// Send a group chat message
ZIMConversationType type = ZIMConversationType.Gourp;
// send room info
ZIMConversationType type = ZIMConversationType.Room;
// 5. Send message
zim.sendMessage(zimMessage, toConversationID, type, config, new ZIMMessageSentCallback() {
@Override
public void onMessageAttached(ZIMMessage zimMessage) {
}
@Override
public void onMessageSent(ZIMMessage zimMessage, ZIMError error) {
// Developers can use this callback to monitor whether the message is sent successfully.
}
});
Configure an extension field visible only to the local end
Procedure for sending a message:
- Log in to ZIM SDK, construct a ZIMMessagefield as an extension field visible only to the local end in the ZIMMessage object.
- Call the sendMessage operation and pass in the ZIMMessage object to send the message and the extension field.
// 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 content";
// Configure the local extension field of a message.
zimTextMessage.localExtendedData = "Local extension field of the message";
ZIMMessageSendConfig config = ZIMMessageSendConfig();
// Configure the priority of the message.
config.priority = ZIMMessagePriority.low;
// Send a private-chat message.
ZIMConversationType type = ZIMConversationType.peer;
// Send a group-chat message.
ZIMConversationType type = ZIMConversationType.group;
// Send a room message.
ZIMConversationType type = ZIMConversationType.room;
// 2. Send the message.
zim.sendMessage(zimMessage, toConversationID, type, config, new ZIMMessageSentCallback() {
@Override
public void onMessageAttached(ZIMMessage zimMessage) {
}
@Override
public void onMessageSent(ZIMMessage zimMessage, ZIMError error) {
// Developers can use this callback to monitor whether the message is sent successfully.
}
});
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:
// After receiving a single chat message, update the local extension field of the message.
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onReceivePeerMessage(ZIM zim, ArrayList<ZIMMessage> messageList, String fromUserID) {
for(ZIMMessage message: messageList){
zim.updateMessageLocalExtendedData("Update local extension fields", message, new ZIMMessageLocalExtendedDataUpdatedCallback() {
@Override
public void onMessageExtendedDataUpdated(ZIMMessage message, ZIMError errorInfo) {
// Developers can use this callback to monitor whether the local extended field is updated successfully.
}
});}
}
});