Message Extension
Overview
Message extension allows you to configure keys and values for messages to implement polling, group notes, survey and other types of messages.
For polling, create a custom message using the
createCustomMessage
API (details), where data
stores the polling title and options. And store the user ID of the voter and selected option(s) in the key
and value
of the message extension, respectively. With the selected options of users, we can calculate the polling percentage in real time.For group notices, create a custom message for group notification using the
createCustomMessage
API, where data
stores the title of the group notice, and then store the user ID and the corresponding info in the key
and value
of the message extension, respectively.For survey, create a custom message using the
createCustomMessage
API, where data
stores the title and options of the survey, and then store the user ID and the corresponding info in the key
and value
of the message extension, respectively.Note:
To use this feature, you need to purchase the Ultimate edition.
This feature is available only in SDK v4.1.8 or later.
You need to enable this feature via Chat console > Feature Configuration > Login and Message > Set message extension.
This feature is not available for communities and audio/video groups.
Setting message extension
Call the
setMessageExtensions
API (details) to set the message extension. If an extension already exists, modify its value
info. Otherwise, add new ones.The request parameters of the
setMessageExtensions
API are detailed as follows:Attribute | Definition | Description |
message | Message object | Three message conditions to meet: The message is sent successfully. The message is not a message of a community/audio-video group. |
extensions | Extensions | Modify the `value` info of an existing extension, or add new extensions. |
Note:
1. The
key
and value
of an extension can contain up to 100 B and 1 KB, respectively. You can set up to 20 extensions each time and 300 extensions for a message.1. If multiple users set or delete the
key
of the same extension simultaneously, only the first user can operate successfully, and other users will receive the error code 23001 and the latest extension info in the setting response packet, who can set it again if necessary.2. We recommend setting unique keys of extensions by different users to avoid conflicts in most cases. For example, the
userID
can be set as the key
of the extension in polling, group notices and survey.Sample code:
// Setting message extensionV2TimValueCallback<List<V2TimMessageExtensionResult>>setMessageExtensionsRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().setMessageExtensions(msgID: '', // ID of the message for extension settingextensions: []); // Message extension fieldif (setMessageExtensionsRes.code == 0) {// Set message extensions successfully}
Getting message extensions
Note:
If the network is unavailable, the SDK will return the message extension list cached locally.
Sample code:
// Get message extensionsV2TimValueCallback<List<V2TimMessageExtension>> getMessageExtensionsRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageExtensions(msgID: '', // ID of the message whose extension information is to be obtained);if (getMessageExtensionsRes.code == 0) {// Got message extensions successfullygetMessageExtensionsRes.data?.forEach((element) {element.extensionKey; // Key of the extension field modifiedelement.extensionValue; // Value of the extension field modified});}
Deleting message extensions
Call the
deleteMessageExtensions
API (details) to delete message extensions. If the value of the keys
field is set to null
, all message extensions will be cleared.Sample code:
// Delete message extensionsV2TimValueCallback<List<V2TimMessageExtensionResult>>deleteMessageExtensionsRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().deleteMessageExtensions(msgID: '', // ID of the message for extension deletionkeys: []); // List of keys of the extension fields to be deletedif (deleteMessageExtensionsRes.code == 0) {// Deleted message extensions successfullydeleteMessageExtensionsRes.data?.forEach((element) {element.extension; // Message extension informationelement.resultCode; // Operation result codeelement.resultInfo; // Result description});}
Updating message extensions
If you have added an event listener for advanced messages by calling
addAdvancedMsgListener
, you will receive the onRecvMessageExtensionsChanged
callback (details) when message extensions are added or updated, and the onRecvMessageExtensionsDeleted
callback (details) when message extensions are deleted.Sample code:
// Create a message listenerV2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvMessageExtensionsChanged:(String msgID, List<V2TimMessageExtension> extensions) {// msgID: ID of the message modified// extensions: List of extension fields modifiedfor (V2TimMessageExtension element in extensions) {element.extensionKey; // Key of the extension field modifiedelement.extensionValue; // Value of the extension field modified}},onRecvMessageExtensionsDeleted: (msgID, extensionKeys) {// msgID: ID of the message whose extension information is deleted// extensionKeys: List of keys whose extension information is deleted},);// Add an event listener for advanced messagesTencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);