// Send a message with a mention (use <@uid:UID> format)TextMessage textMessage = TextMessage( text: "Hello <@uid:cometchat-uid-1>", receiverUid: "UID", receiverType: CometChatReceiverType.user, type: CometChatMessageType.text,);CometChat.sendMessage(textMessage, onSuccess: (msg) { debugPrint("Mentioned users: ${msg.mentionedUsers}");}, onError: (e) {});// Get mentioned users from a messageList<User> mentionedUsers = message.mentionedUsers;// Check if logged-in user was mentionedbool wasMentioned = message.hasMentionedMe ?? false;// Fetch messages with mention tag infoMessagesRequest request = (MessagesRequestBuilder() ..uid = "UID" ..limit = 30 ..mentionsWithTagInfo(true)).build();request.fetchPrevious(onSuccess: (messages) {}, onError: (e) {});
Mentions in messages enable users to refer to specific individuals within a conversation. This is done by using the <@uid:UID> format, where UID represents the user’s unique identification.Mentions are a powerful tool for enhancing communication in messaging platforms. They streamline interaction by allowing users to easily engage and collaborate with particular individuals, especially in group conversations.
Every User object has a String unique identifier associated with them which can be found in a property called uid. To mention a user in a message, the message text should contain the uid in following format: <@uid:UID_OF_THE_USER>. For example, to mention the user with UID cometchat-uid-1 in a text message, your text should be “<@uid:cometchat-uid-1>”
By default, the SDK will fetch all the messages irrespective of the fact that the logged-in user is mentioned or not in the message. The SDK has other optional filters such as tag info and blocked info.
Setting
Description
Default Value
mentionsWithTagInfo(bool value)
If set to true, SDK will fetch a list of messages where users are mentioned & will also fetch the tags of the mentioned users.
false
mentionsWithBlockedInfo(bool value)
If set to true, SDK will fetch a list of messages where users are mentioned & will also fetch their blocked relationship with the logged-in user.
To get a list of messages in a conversation where users are mentioned along with the tags of the mentioned users.Relevant fields to access on returned messages and their mentioned users:
To get a list of messages in a conversation where users are mentioned along with the blocked relationship of the mentioned users with the logged-in user.Relevant fields to access on returned messages and their mentioned users:
Whether the logged-in user has blocked this mentioned user
hasBlockedMe (on each mentioned user)
hasBlockedMe
bool
Whether this mentioned user has blocked the logged-in user
Flutter (User)
Flutter (Group)
String UID = "cometchat-uid-1";MessagesRequest messagesRequest = (MessagesRequestBuilder()..limit=50..uid=UID..mentionsWithBlockedInfo(true)).build();messagesRequest.fetchPrevious(onSuccess:(List<BaseMessage> messages) { for (BaseMessage message in messages){ for (User user in message.mentionedUsers){ print("mentioned user has blocked me? ${user.hasBlockedMe}"); print("mentioned user is blocked by me? ${user.blockedByMe}"); } } }, onError: (CometChatException e) { print("error: $e"); });
String GUID = "cometchat-guid-1";MessagesRequest messagesRequest = (MessagesRequestBuilder()..limit=50..guid=GUID..mentionsWithBlockedInfo(true)).build();messagesRequest.fetchPrevious(onSuccess:(List<BaseMessage> messages) { for (BaseMessage message in messages){ for (User user in message.mentionedUsers){ print("mentioned user has blocked me? ${user.hasBlockedMe}"); print("mentioned user is blocked by me? ${user.blockedByMe}"); } } }, onError: (CometChatException e) { print("error: $e"); });
Response
On Success — A List<BaseMessage> containing messages with mentioned users and their blocked relationship info (each item is a BaseMessage object):BaseMessage Object:
To retrieve the list of users mentioned in the particular message, you can use the mentionedUsers property on any BaseMessage. This property will return an array containing User objects of the mentioned users, or an empty array if no users were mentioned in the message.
Dart
message.mentionedUsers
The mentionedUsers property returns a List<User> objects.
To check if the logged-in user has been mentioned in a particular message we can use the hasMentionedMe property on any BaseMessage. This property will return a boolean value, true if the logged-in user has been mentioned, otherwise false.