Skip to main content
Version: v4

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>"

let receiverID = "UID"
let messageText = "Hello, <@uid:cometchat-uid-1>"
let receiverType = CometChat.ReceiverType.user

let textMessage = TextMessage(receiverUid: receiverID, text: messageText, receiverType: receiverType)

CometChat.sendTextMessage(message:textMessage){ textMessage in
print("Message sent successfully: (textMessage.text), mentioned users: (textMessage.getMentionedUsers())")
} onError: { error in
print("Message sending failed with exception: (error?.errorDescription)")
}
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 tag info and blocked info.

SettingDescription
mentionsWithTagInfo(bool value)If set to true, SDK will fetch a list of messages where users are mentioned and will also fetch the tags of the mentioned users. Default value is false.
mentionsWithBlockedInfo(bool value)If set to true, SDK will fetch a list of messages where users are mentioned and will also fetch their blocked relationship with the logged-in user. Default value is false.

Mentions With Tag Info

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

let UID = "cometchat-uid-1";

let messagesRequest = MessagesRequest(builder: MessagesRequest.MessageRequestBuilder().set(uid: UID).set(limit: 50).mentionsWithTagInfo(true))


messagesRequest.fetchPrevious { fetchedMessages in
if let messages = fetchedMessages{
for message in messages{
for user in message.mentionedUsers{
print("mentioned user tags: \(user.tags)")
}
}
}
} onError: { error in
if let err = error{
print("\(err.errorDescription)")
}
}

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 = "cometchat-uid-1";

let messagesRequest = MessagesRequest(builder: MessagesRequest.MessageRequestBuilder().set(uid: UID).set(limit: 50).mentionsWithBlockedInfo(true))


messagesRequest.fetchPrevious { fetchedMessages in
if let messages = fetchedMessages{
for message in messages{
for user in message.mentionedUsers{
print("mentioned user has blocked me? \(user.hasBlockedMe)")
print("mentioned user is blocked by me? \(user.blockedByMe)")
}
}
}
} onError: { error in
if let err = error{
print("\(err.errorDescription)")
}
}

Get Users Mentioned In a Particular Message

To retrieve the list of users mentioned in the particular message, you can use the mentionedUsers method on any BaseMessage. This method will return an array containing User objects of the mentioned users, or an empty array if no users were mentioned in the message.

message.mentionedUsers

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 mentionedMe method on any BaseMessage. This method will return a boolean value, true if the logged-in user has been mentioned, otherwise false.

message.mentionedMe