Skip to main content
Version: v4

Mentions

Mentions in messages enable users to refer to specific individual 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.

Send Mentioned Messages

To send a message with a mentioned user, you must follow a specific format: <@uid:UID>. For example, to mention the user with UID cometchat-uid-1 with the message "Hello," your text would be "Hello, <@uid:cometchat-uid-1>"

let receiverID = "UID";
let messageText = "Hello, <@uid:cometchat-uid-1>";
let receiverType = CometChat.RECEIVER_TYPE.USER;
let textMessage = new CometChat.TextMessage(receiverID, messageText, receiverType);

CometChat.sendMessage(textMessage).then(
message => {
console.log("Message sent successfully:", message);
}, error => {
console.log("Message sending failed with error:", error);
}
);
Note

You can mention user in text message and media messages captions

Mentioned Messages

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 allows you to fetch messages in a conversation where the logged-in user is mentioned. The SDK also has other optional filters such as tags and blocked relationships.

SettingDescription
myMentionsOnly(boolean value)If set to true, SDK will fetch a list of messages where the logged-in user is mentioned.
Default value = false
mentionsWithTagInfo(boolean 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.
Default value = false
mentionsWithBlockedInfo(boolean 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.
Default value = false

My Mentions Only

To get a list of messages in a conversation where the logged-in user is mentioned.

let UID = "UID";
let limit = 30;

var messagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setLimit(limit)
.myMentionsOnly(true)
.build();

messagesRequest.fetchPrevious().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);

Mentions With Tag Info

To get a list of messages in a conversation where users are mentioned along with the user tags of the mentioned users.

let UID = "UID";
let limit = 30;

var messagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setLimit(limit)
.mentionsWithTagInfo(true)
.build();

messagesRequest.fetchPrevious().then(
messages => {
messages.forEach((eachMessage) => {
eachMessage.getMentionedUsers().forEach(eachMentionedUser => {
console.log(eachMentionedUser.getTags());
})
});
}, error => {
console.log("Message fetching failed with error:", error);
}
);

Mentions With Blocked Info

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.

let UID = "UID";
let limit = 30;

var messagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setLimit(limit)
.mentionsWithBlockedInfo(true)
.build();

messagesRequest.fetchPrevious().then(
messages => {
messages.forEach((eachMessage) => {
eachMessage.getMentionedUsers().forEach(eachMentionedUser => {
console.log( "blockedByMe: " + eachMentionedUser.getBlockedByMe());
console.log( "hasBlockedMe: " + eachMentionedUser.getHasBlockedMe());
})
});
}, error => {
console.log("Message fetching failed with error:", error);
}
);

Get Users Mentioned In a Particular Message

To retrieve the list of users mentioned in the particular message, you can use the message.getMentionedUsers() method. This method will return an array containing the mentioned users, or an empty array if no users were mentioned in the message.

message.getMentionedUsers()