Skip to main content

Mentions

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. Mentions in messages enable users to refer to specific individuals within a conversation.

Send Mentioned Messages

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>"
Starting with v5.1.0, the CallbackListener overloads of `sendMessage()` are deprecated. Use MessageSentListener instead; it requires SDK 5.1.0 or later. On 5.0.x, keep using CallbackListener and handle the sent message in onSuccess where these examples use onMessageSent. With offline support off, it behaves exactly like the old callback: onError is unchanged, and the sent message arrives in onMessageSent instead of onSuccess. With offline support on, onMessagePending fires first. A message sent without a connection stays PENDING and is sent when the connection returns: onError fires with code ERROR_INTERNET_UNAVAILABLE as a not-connected notice, and onMessageSent follows once the message is sent. In onError, check the message’s getStatus(): PENDING means queued, FAILED means it failed. The old CallbackListener overloads are never queued; offline, they end with onError. See Send While Offline.
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 has other optional filters such as tag info and blocked info.

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.
Starting with v5.1.0, the CallbackListener overload of `fetchPrevious()` / `fetchNext()` is deprecated. Use `MessageFetchListener` instead; it requires SDK 5.1.0 or later, so on 5.0.x keep using CallbackListener. With offline support off, it behaves exactly like the old callback: onError is unchanged, and the server result arrives in the onServer… callback instead of onSuccess. With offline support on, it also delivers the cached result first; the old overload keeps working but receives only the server result. See Show Cached Data.

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.

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.

Check if Logged-in user has been mentioned

To check if the logged-in user has been mentioned in a particular message we can use the hasMentionedMe() method on any BaseMessage. This method will return a boolean value, true if the logged-in user has been mentioned, otherwise false.

Offline Behaviour

With offline support enabled, these mention fetches run in two phases: onCachedMessages delivers the cached page first (even when empty), then onServerMessages delivers the reconciled server copy. Offline, the fetch ends with onError with code ERROR_INTERNET_UNAVAILABLE after the cached page. See Show Cached Data. mentionsWithTagInfo and mentionsWithBlockedInfo enrich a read without narrowing it, so they never empty the cached page: the cached phase serves the matching messages as stored, and the server phase supplies the mentioned users’ tag and blocked-relationship detail.