logo
In-app Chat
SDK Error Codes
Powered Byspreading
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.
title
// 1. Create a ZIM object and pass in appID and appSign
ZIMAppConfig *appConfig = [[ZIMAppConfig alloc] init];
appConfig.appID = (unsigned int)appID;    // 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
self.zim = [ZIM createWithAppConfig: appConfig];

// 2. Set the setEventHandler callback
[self.zim setEventHandler:self];

// 3. Login
ZIMUserInfo *userInfo = [[ZIMUserInfo alloc] init];
userInfo.userID = @"xxxx";
userInfo.userName = @"xxxx";
[self.zim loginWithUserInfo:userInfo callback:^(ZIMError * _Nonnull errorInfo){
    // Developers can judge whether the login is successful according to ZIMError.
}];

NSString *toConversationID = @"xxxx1";

ZIMTextMessage *textMessage = [[ZIMTextMessage alloc] init];
textMessage.message = @"Message content";
textMessage.extendedData = @"Extended field"; // The extended field that needs to be sent

ZIMMessageSendConfig *config = [[ZIMMessageSendConfig alloc] init];

ZIMMessageSendNotification *notification = [[ZIMMessageSendNotification alloc] init];
notification.onMessageAttached = ^(ZIMMessage * _Nonnull message) {
    // The callback before sending, where the client can obtain a temporary object, which belongs to the same object as the zimMessage object created by the developer. The developer can use this feature to do some business logic, such as displaying the UI in advance, etc.
};

// 4. Set the session type and select it to send messages to the corresponding session type
// Send one-on-one chat information
ZIMConvesationType type = ZIMConversationTypePeer;

// Send a group chat message
ZIMConvesationType type = ZIMConversationTypeGroup;

// send room info
ZIMConvesationType type = ZIMConversationTypeRoom;

// send messages
[self.zim sendMessage:textMessage toConversationID:toConversationID conversationType:type config:config notification:notification callback:^((ZIMMessage * _Nonnull message, ZIMError * _Nonnull errorInfo)) {
    // Developers can use this callback to monitor whether the message is sent successfully.
}];
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.
NSString *toConversationID = @"xxx1";

// Construct a ZIMMessage object.
ZIMTextMessage *zimTextMessage = [[ZIMTextMessage alloc] initWithMessage:@"Message content"];
// Configure the local extension field of a message.zimTextMessage.localExtendedData = @"Local extension field of the message";
ZIMMessageSendConfig *config = [[ZIMMessageSendConfig alloc] init];
// Configure the priority of the message.
config.priority = ZIMMessagePriorityLow;
    
// Set the type of session sent.
// Send a private-chat message.
ZIMConversationType type = ZIMConversationTypePeer;
    
ZIMMessageSendNotification *notification = [[ZIMMessageSendNotification alloc] init];
notification.onMessageAttached = ^(ZIMMessage *message){
        
};
    
[[ZIM getInstance] sendMessage:zimTextMessage toConversationID:toConversationID conversationType:type config:config notification:notification callback:^(ZIMMessage * _Nonnull message, ZIMError * _Nonnull 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
// Update the local extension field of a private-chat message after you receive the message.
- (void)zim:(ZIM *)zim receivePeerMessage:(NSArray<ZIMMessage *> *)messageList fromUserID:(NSString *)fromUserID {
    NSArray *messageBasicList = [ZGZIMManager cnvZIMMessageListToDicList:messageList];
    GGLog(@"[GGLog][event][receivePeerMessage],fromUserID:%@,messageList:%@",fromUserID,messageBasicList);
    for (ZIMMessage *message in messageList) {
        [[ZIM getInstance] updateMessageLocalExtendedData:@"Update local extension fields" message:message callback:^(ZIMMessage * _Nonnull message, ZIMError * _Nonnull errorInfo) {
            // Developers can use this callback to monitor whether the local extended field is updated successfully.        }];
    }
}
1
Copied!

Previous

Read receipts

Next

Search for local messages