addAdvancedMsgListener
API, with the callbacks defined in the protocol of V2TimAdvancedMsgListener
.addAdvancedMsgListener
API (details) to add the advanced message listener. It is recommended to call the API early (such as after the chat panel is initialized) to ensure timely message receiving in the application.// Create a message listenerV2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {// Callback after marking a one-to-one message as read},onRecvMessageModified: (V2TimMessage message) {// `msg` is the modified message object},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {// Callback after marking a group message as readreceiptList.forEach((element) {element.groupID; // Group IDelement.msgID; // Message IDelement.readCount;// Latest group message read countelement.unreadCount;// Latest group message unread countelement.userID; // ID of the other party of the one-to-one message});},onRecvMessageRevoked: (String messageid) {// Process the recalled messages among locally maintained messages},onRecvNewMessage: (V2TimMessage message) async {// Process a text messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_TEXT) {message.textElem?.text;}// Use a custom messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_CUSTOM) {message.customElem?.data;message.customElem?.desc;message.customElem?.extension;}// Use an image messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_IMAGE) {message.imageElem?.path; // Image upload path. This field applies only to the message sender who can use it to display the image on the screen in advance to optimize the experiencemessage.imageElem?.imageList?.forEach((element) {// Traverse large images, original images, and thumbnails// Parse image attributeselement?.height;element?.localUrl;element?.size;element?.type; // Large image, original image, or thumbnailelement?.url;element?.uuid;element?.width;});}// Process a video messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_VIDEO) {// Parse the video message attributes such as thumbnail, playback address, width and height, and sizemessage.videoElem?.UUID;message.videoElem?.duration;message.videoElem?.localSnapshotUrl;message.videoElem?.localVideoUrl;message.videoElem?.snapshotHeight;message.videoElem?.snapshotPath;// ...}// Process an audio messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_SOUND) {// Parse the playback address, local address, size, and duration of the audio message.message.soundElem?.UUID;message.soundElem?.dataSize;message.soundElem?.duration;message.soundElem?.localUrl;message.soundElem?.url;// ...}// Process a file messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FILE) {// Parse the filename, size, URL and other info of the file messagemessage.fileElem?.UUID;message.fileElem?.fileName;message.fileElem?.fileSize;message.fileElem?.localUrl;message.fileElem?.path;message.fileElem?.url;}// Process a location messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_LOCATION) {// Parse the geographical location information such as latitude, longitude, and descriptionmessage.locationElem?.desc;message.locationElem?.latitude;message.locationElem?.longitude;}// Process an emoji messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FACE) {message.faceElem?.data;message.faceElem?.index;}// Process a group tip text messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS) {message.groupTipsElem?.groupID; // Groupmessage.groupTipsElem?.type; // Group tip typemessage.groupTipsElem?.opMember; // Operator profilemessage.groupTipsElem?.memberList; // User profile being operatedmessage.groupTipsElem?.groupChangeInfoList; // Group information change detailsmessage.groupTipsElem?.memberChangeInfoList; // Group member change informationmessage.groupTipsElem?.memberCount; // Number of current online group members}// Process a combined messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_MERGER) {message.mergerElem?.abstractList;message.mergerElem?.isLayersOverLimit;message.mergerElem?.title;V2TimValueCallback<List<V2TimMessage>> download =await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMergerMessage(msgID: message.msgID!,);if (download.code == 0) {List<V2TimMessage>? messageList = download.data;}}if (message.textElem?.nextElem != null) {// Get the next element object through the `nextElem` method of the first element object. If the next object exists, an element object instance is returned; otherwise, `null` is returned.}},onSendMessageProgress: (V2TimMessage message, int progress) {// File upload progress callback},);// Add an event listener for advanced messagesTencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
removeAdvancedMsgListener
API (details) to remove the advanced message listener.// Create a listenerV2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) {},onSendMessageProgress: (V2TimMessage message, int progress) {},);// Register the listenerTencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);// Remove the listenerTencentImSDKPlugin.v2TIMManager.getMessageManager().removeAdvancedMsgListener(listener: listener);
addAdvancedMsgListener
to set the event listener.onRecvNewMessage
callback (details) that contains the custom message.removeAdvancedMsgListener
to remove the listener. This step is optional and can be performed as needed.V2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) {// Process a text messageif(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_TEXT){String text = message.textElem.text;}},onSendMessageProgress: (V2TimMessage message, int progress) {},);TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
addAdvancedMsgListener
to set the event listener.onRecvNewMessage
callback (details) that contains the custom message.removeAdvancedMsgListener
to remove the listener. This step is optional and can be performed as needed.V2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) {// Use a custom messageif(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_CUSTOM){String data = message.customElem.data;String desc = message.customElem.desc;String ext = message.customElem.extension;}},onSendMessageProgress: (V2TimMessage message, int progress) {},);TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
addAdvancedMsgListener
API to set the advanced message listener.onRecvNewMessage
callback (details) to get the V2TimMessage
message.elemType
attribute in the V2TIMMessage
message, and then parse the message again according to the type to get the specific content of the elements in the message.removeAdvancedMsgListener
to remove the listener. This step is optional and can be performed as needed.Large
(large image): A large image is an image obtained after the original image is proportionally compressed. After the compression, the height or width (whichever is shorter) is equal to 720 pixels.
Thumb
(thumbnail): A thumbnail is an image obtained after the original image is proportionally compressed. After the compression, the height or width (whichever is shorter) is equal to 198 pixels.url
) will not be returned when users get the message list. They need to be obtained by calling the getMessageOnlineUrl
API (details).
In the SDK for Flutter SDK 5.0.4 or later, multimedia message local URLs (localurl
) will no longer be returned by default. They will be returned only after you download messages successfully by calling the downloadMessage
API (details).V2TimMessage
:V2TimAdvancedMsgListener listener = V2TimAdvancedMsgListener(onRecvC2CReadReceipt: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageModified: (V2TimMessage message) {},onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {},onRecvMessageRevoked: (String messageid) {},onRecvNewMessage: (V2TimMessage message) async {// Use an image messageif (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_IMAGE) {String path = message.imageElem.path; // Image upload path. This field applies only to the message sender who can use it to display the image on the screen in advance to optimize the experiencefor (var item in message.imageElem.imageList) { // Traverse large images, original images, and thumbnails// Parse image attributesint height = item.height;String localUrl = item.localUrl;int size = item.size;int type = item.type; // Large image, thumbnail, or original imageString url = item.url;String uuid = item.uuid;int width = item.width;}}// Get the multimedia message URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // Message ID);if (getMessageOnlineUrlRes.code == 0) {// Obtained successfully}// Download a multimedia messageV2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // Message IDmessageType: 3, // Message typeimageType: 0, // Image type. This field is valid only for image messages.isSnapshot: false // Whether it is a video thumbnail. This field is valid only for video messages.);if (downloadMessageRes.code == 0) {// Download successful}},onSendMessageProgress: (V2TimMessage message, int progress) {},);TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: listener);
V2TimMessage
:url
) will not be returned when users get the message list. They need to be obtained by calling the getMessageOnlineUrl
API (details).
In the SDK for Flutter SDK 5.0.4 or later, multimedia message local URLs (localurl
) will no longer be returned by default. They will be returned only after you download messages successfully by calling the downloadMessage
API (details).if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_VIDEO){// Parse the video message attributes such as thumbnail, playback address, width and height, and sizemessage.videoElem.UUID;message.videoElem.duration;message.videoElem.localSnapshotUrl;message.videoElem.localVideoUrl;message.videoElem.snapshotHeight;message.videoElem.snapshotPath;// ...}// Get the multimedia message URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // Message ID);if (getMessageOnlineUrlRes.code == 0) {// Obtained successfully}// Download a multimedia messageV2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // Message IDmessageType: 5, // Message typeimageType: 0, // Image type. This field is valid only for image messages.isSnapshot: false // Whether it is a video thumbnail. This field is valid only for video messages.);if (downloadMessageRes.code == 0) {// Download successful}
V2TimMessage
:url
) will not be returned when users get the message list. They need to be obtained by calling the getMessageOnlineUrl
API (details).
In the SDK for Flutter SDK 5.0.4 or later, multimedia message local URLs (localurl
) will no longer be returned by default. They will be returned only after you download messages successfully by calling the downloadMessage
API (details).if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_SOUND){// Parse the playback address, local address, size, and duration of the audio message.message.soundElem.UUID;message.soundElem.dataSize;message.soundElem.duration;message.soundElem.localUrl;message.soundElem.url;// ...// Get the multimedia message URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // Message ID);if (getMessageOnlineUrlRes.code == 0) {// Obtained successfully}// Download a multimedia messageV2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // Message IDmessageType: 4, // Message typeimageType: 0, // Image type. This field is valid only for image messages.isSnapshot: false // Whether it is a video thumbnail. This field is valid only for video messages.);if (downloadMessageRes.code == 0) {// Download successful}}
V2TimMessage
:url
) will not be returned when users get the message list. They need to be obtained by calling the getMessageOnlineUrl
API (details).
In the SDK for Flutter SDK 5.0.4 or later, multimedia message local URLs (localurl
) will no longer be returned by default. They will be returned only after you download messages successfully by calling the downloadMessage
API (details).if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FILE){// Parse the filename, size, URL and other info of the file messagemessage.fileElem.UUID;message.fileElem.fileName;message.fileElem.fileSize;message.fileElem.localUrl;message.fileElem.path;message.fileElem.url;// Get the multimedia message URLV2TimValueCallback<V2TimMessageOnlineUrl> getMessageOnlineUrlRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().getMessageOnlineUrl(msgID: '', // Message ID);if (getMessageOnlineUrlRes.code == 0) {// Obtained successfully}// Download a multimedia messageV2TimCallback downloadMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().downloadMessage(msgID: '', // Message IDmessageType: 6, // Message typeimageType: 0, // Image type. This field is valid only for image messages.isSnapshot: false // Whether it is a video thumbnail. This field is valid only for video messages.);if (downloadMessageRes.code == 0) {// Download successful}}
locationElem
.
The following sample code demonstrates how to parse the geographical location content from V2TimMessage
:if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_LOCATION){// Parse the geographical location information such as latitude, longitude, and descriptionmessage.locationElem.desc;message.locationElem.latitude;message.locationElem.longitude;}
faceElem
(details), where the contents of index
and data
are customized by the user.index
to 1
and data
to x12345
to indicate the smile emoji.
The receiver parses the received emoji message as 1
and x12345
and displays the message as the smile emoji according to the preset rules.V2TIMMessage
:if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_FACE) {message.faceElem.data;message.faceElem.index;}
V2TIMGroupTipsType
(details).V2TIMGroupTipsElem
.type
.V2TIM_GROUP_TIPS_TYPE_GROUP_INFO_CHANGE
is parsed from the type
, indicating that this is a notification of group profile change.
The receiver can get the operator information from opMember
and the new group name from groupChangeInfoList
.
At this point, the receiver can merge the "operator" and "new group name" to create a group tip, such as "Alice renamed the group as "group123"".V2TIMMessage
:if(message.elemType == MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS){message.groupTipsElem.groupID; // Groupmessage.groupTipsElem.type; // Group tip typemessage.groupTipsElem.opMember; // Operator profilemessage.groupTipsElem.memberList; // User profile being operatedmessage.groupTipsElem.groupChangeInfoList; // Group information change detailsmessage.groupTipsElem.memberChangeInfoList; // Group member change informationmessage.groupTipsElem.memberCount; // Number of current online group members}
Message
object.nextElem
method of the first element object. If the next object exists, an element object instance is returned; otherwise, null
is returned.if(message.textElem.nextElem!=null){// There is a next message}
Was this page helpful?