Mentions
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>"
- Java(User)
- Kotlin(User)
- Java(Group)
- Kotlin(Group)
private String receiverID = "UID";
private String messageText = "Hello, <@uid:cometchat-uid-1>";
private String receiverType = CometChatConstants.RECEIVER_TYPE_USER;
TextMessage textMessage = new TextMessage(receiverID, messageText, receiverType);
CometChat.sendMessage(textMessage, new CometChat.CallbackListener<TextMessage>() {
@Override
public void onSuccess(TextMessage textMessage) {
Log.d(TAG, "Message sent successfully: " + textMessage.toString());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, "Message sending failed with exception: " + e.getMessage());
}
});
private val receiverID = "UID"
private val messageText = "Hello <@uid:cometchat-uid-1>"
private val receiverType = CometChatConstants.RECEIVER_TYPE_USER
val textMessage = TextMessage(receiverID, messageText, receiverType)
CometChat.sendMessage(textMessage, object : CallbackListener<TextMessage>() {
override fun onSuccess(textMessage: TextMessage) {
Log.d(TAG, "Message sent successfully: $textMessage")
}
override fun onError(e: CometChatException) {
Log.d(TAG, "Message sending failed with exception: " + e.message)
}
})
private String receiverID = "GUID";
private String messageText = "Hello <@uid:cometchat-uid-1>";
private String receiverType = CometChatConstants.RECEIVER_TYPE_GROUP;
TextMessage textMessage = new TextMessage(receiverID, messageText, receiverType);
CometChat.sendMessage(textMessage, new CometChat.CallbackListener<TextMessage>() {
@Override
public void onSuccess(TextMessage textMessage) {
Log.d(TAG, "Message sent successfully: " + textMessage.toString());
}
@Override
public void onError(CometChatException e) {
Log.d(TAG, "Message sending failed with exception: " + e.getMessage());
}
});
private val receiverID = "GUID"
private val messageText = "Hello <@uid:cometchat-uid-1>"
private val receiverType = CometChatConstants.RECEIVER_TYPE_GROUP
val textMessage = TextMessage(receiverID, messageText, receiverType)
CometChat.sendMessage(textMessage, object : CallbackListener<TextMessage>() {
override fun onSuccess(textMessage: TextMessage) {
Log.d(TAG, "Message sent successfully: $textMessage")
}
override fun onError(e: CometChatException) {
Log.d(TAG, "Message sending failed with exception: " + e.message)
}
})
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.
Setting | Description |
---|---|
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. |
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.
- Java(User)
- Kotlin(User)
- Java(Group)
- Kotlin(Group)
String UID = "cometchat-uid-1";
MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setUID(UID)
.mentionsWithTagInfo(true)
.build();
messagesRequest.fetchPrevious(new CometChat.CallbackListener<List<BaseMessage>>() {
@Override
public void onSuccess(List<BaseMessage> messages) {
for (BaseMessage messageObj: messages){
for (User user: messageObj.getMentionedUsers()){
Log.e(TAG, "tag: " + user.getTags());
}
}
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "onError: " + e);
}
});
val UID = "cometchat-uid-1"
val messagesRequest = MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setUID(UID)
.mentionsWithTagInfo(true)
.build()
messagesRequest.fetchPrevious(object : CometChat.CallbackListener<List<BaseMessage>>() {
override fun onSuccess(messages: List<BaseMessage>) {
for (messageObj in messages) {
for (user in messageObj.mentionedUsers) {
Log.e(TAG, "tag: ${user.tags}")
}
}
}
override fun onError(e: CometChatException) {
Log.e(TAG, "onError: " + e)
}
})
String GUID = "cometchat-guid-1";
MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setGUID(GUID)
.mentionsWithTagInfo(true)
.build();
messagesRequest.fetchPrevious(new CometChat.CallbackListener<List<BaseMessage>>() {
@Override
public void onSuccess(List<BaseMessage> messages) {
for (BaseMessage messageObj: messages){
for (User user: messageObj.getMentionedUsers()){
Log.e(TAG, "tag: " + user.getTags());
}
}
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "onError: " + e);
}
});
val GUID = "cometchat-guid-1"
val messagesRequest = MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setGUID(GUID)
.mentionsWithTagInfo(true)
.build()
messagesRequest.fetchPrevious(object : CometChat.CallbackListener<List<BaseMessage>>() {
override fun onSuccess(messages: List<BaseMessage>) {
for (messageObj in messages) {
for (user in messageObj.mentionedUsers) {
Log.e(TAG, "tag: ${user.tags}")
}
}
}
override fun onError(e: CometChatException) {
Log.e(TAG, "onError: " + e)
}
})
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.
- Java(User)
- Kotlin(User)
- Java(Group)
- Kotlin(Group)
String UID = "cometchat-uid-1";
MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setUID(UID)
.mentionsWithBlockedInfo(true)
.build();
messagesRequest.fetchPrevious(new CometChat.CallbackListener<List<BaseMessage>>() {
@Override
public void onSuccess(List<BaseMessage> messages) {
for (BaseMessage messageObj: messages){
for (User user: messageObj.getMentionedUsers()){
Log.e(TAG, "isBlockedByMe: " + user.isBlockedByMe());
Log.e(TAG, "isHasBlockedMe: " + user.isHasBlockedMe());
}
}
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "onError: " + e);
}
});
val UID = "cometchat-uid-1"
val messagesRequest = MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setUID(UID)
.mentionsWithBlockedInfo(true)
.build()
messagesRequest.fetchPrevious(object : CometChat.CallbackListener<List<BaseMessage>>() {
override fun onSuccess(messages: List<BaseMessage>) {
for (messageObj in messages) {
for (user in messageObj.mentionedUsers) {
Log.e(TAG, "isBlockedByMe: ${user.isBlockedByMe}")
Log.e(TAG, "isHasBlockedMe: ${user.isHasBlockedMe}")
}
}
}
override fun onError(e: CometChatException) {
Log.e(TAG, "onError: " + e)
}
})
String GUID = "cometchat-guid-1";
MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setGUID(GUID)
.mentionsWithBlockedInfo(true)
.build();
messagesRequest.fetchPrevious(new CometChat.CallbackListener<List<BaseMessage>>() {
@Override
public void onSuccess(List<BaseMessage> messages) {
for (BaseMessage messageObj: messages){
for (User user: messageObj.getMentionedUsers()){
Log.e(TAG, "isBlockedByMe: " + user.isBlockedByMe());
Log.e(TAG, "isHasBlockedMe: " + user.isHasBlockedMe());
}
}
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "onError: " + e);
}
});
val GUID = "cometchat-guid-1"
val messagesRequest = MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setGUID(GUID)
.mentionsWithBlockedInfo(true)
.build()
messagesRequest.fetchPrevious(object : CometChat.CallbackListener<List<BaseMessage>>() {
override fun onSuccess(messages: List<BaseMessage>) {
for (messageObj in messages) {
for (user in messageObj.mentionedUsers) {
Log.e(TAG, "isBlockedByMe: ${user.isBlockedByMe}")
Log.e(TAG, "isHasBlockedMe: ${user.isHasBlockedMe}")
}
}
}
override fun onError(e: CometChatException) {
Log.e(TAG, "onError: " + e)
}
})
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.
- Java
- Kotlin
message.getMentionedUsers()
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 hasMentionedMe()
method on any BaseMessage
. This method will return a boolean value, true
if the logged-in user has been mentioned, otherwise false
.
- Java
- Kotlin
message.hasMentionedMe()
message.hasMentionedMe