Mention users
The Mentions feature lets users tag specific individuals within a chat or conversation.
Unity Chat SDK lets one user tag another user by adding @ and typing at least three first letters of the username they want to mention. As a result, they get a list of suggested usernames when typing such a suggestion, like @Mar.
Generic referencing
Channel references, user mentions, and links are instances of MessageElement with different MentionTarget types.
The list of returned users depends on your app configuration - these can be either all members in the channel where you write a message or all users of your app (user data taken from the Admin Portal keyset for your app). The number of returned suggested users for the mention also depends on your app configuration and can show up to 100 suggestions. The names of the suggested users can consist of multiple words and contain up to 200 characters.
You can configure your app to let users mention up to 100 users in a single message (default value is 10).
You can implement mentions in your app in a similar way you would implement channel referencing and links.
Requires App Context
To mention users from a keyset, you must enable App Context for your app's keyset in the Admin Portal.
Add user mentions
You can let users mention other users in a message by adding @ and typing at least three first letters of the username they want to mention, like @Mar.
Whenever you mention a user, this user is added to the list of all mentioned users inside the MessageDraft object. This draft contains the text content and all mentioned users and their names from the selected user metadata source (all channel members or all users on the app's keyset). Once you send this message (Send()), that information gets stored in the message metadata.
Method signature
You can add a user reference by calling the AddMention() method with the target of MentionTarget.User.
Refer to the AddMention() method for details.
Sample code
Create the Hello Alex! I have sent you this link on the #offtopic channel. message where Alex is a user mention.
1
Remove user mentions
RemoveMention() lets you remove a previously added user mention from a draft message.
Method signature
You can remove user mentions from a draft message by calling the RemoveMention() method at the exact offset where the user mention starts.
Refer to the RemoveMention() method for details.
Offset value
If you don't provide the position of the first character of the message element to remove, it isn't removed.
Sample code
Remove the user mention from the Hello Alex! I have sent you this link on the #offtopic channel. message where Alex is a user mention.
1
Get user suggestions
The event handler you attached to the OnDraftUpdated event returns all users referenced in the draft message that match the provided 3-letter string from your app's keyset.
For example, if you type Sam, you will get the list of users starting with Sam like Samantha or Samir. The default number of returned suggested usernames is 10 which is configurable to a maximum value of 100.
Method signature
You must handle the event to update to the contents of a message draft, as well as retrieve the current message elements suggestions for user mentions, channel reference, and links. For example, when the message draft contains ... @name ... or ... #chann ....
Enable receiving suggested mentions
You must enable receiving suggested mentions by setting messageDraft.ShouldSearchForSuggestions = true; before introducing your event handler.
Refer to the Add a message draft listener section for details.
Sample code
Insert the first suggested mention whenever the draft is updated and mentions are detected.
1
Get mentioned users
You can access the MentionedUsers property of the Message object to return all users mentioned in a message.
Method signature
This is how you can access the property:
1message.MentionedUsers
Properties
| Property | Description | 
|---|---|
| MentionedUsersType:  List<User> | List of all users mentioned in a message. | 
Sample code
Check if the message with the 16200000000000000 timetoken contains any mentions.
1
Collect all user-related mentions
The GetCurrentUserMentions() method lets you collect in one place all instances when a specific user was mentioned by someone — either in channels or threads. You can use this info to create a channel with all user-related mentions.
Method signature
This method has the following signature:
1chat.GetCurrentUserMentions(string startTimeToken, string endTimeToken, int count)
Input
| Parameter | Description | 
|---|---|
| startTimetoken*Type:  stringDefault: n/a | Timetoken delimiting the start of a time slice (exclusive) to pull messages with mentions from. For details, refer to the Fetch History section. | 
| endTimetoken*Type:  stringDefault: n/a | Timetoken delimiting the end of a time slice (inclusive) to pull messages with mentions from. For details, refer to the Fetch History section. | 
| count*Type:  intDefault: 100 | Number of historical messages with mentions to return in a single call. Since each call returns all attached message actions by default, the maximum number of returned messages is 100. For more details, refer to the description of theIncludeMessageActionsparameter in the Unity SDK docs. | 
Output
This method returns an awaitable Task<UserMentionsWrapper> with the wrapper object containing two fields: Mentions and IsMore.
| Parameter | Description | 
|---|---|
| MentionsType:  List<UserMentionData> | Array listing the requested number of historical mention events with a set of information that differ slightly depending on whether you were mentioned in the main (parent) channel or in a thread. For mentions in the parent channel, the returned UserMentionDataincludes these fields:ChannelIdwhere you were mentioned,userIdthat mentioned you,Event(of typeMention),Messagethat included the mention.For mentions in threads, the returned UserMentionDataincludes similar fields, the only difference is that you'll getParentChannelIdandThreadChannelIdfields instead of justChannelIdto clearly differentiate the thread that included the mention from the parent channel in which this thread was created. | 
| IsMoreType:  bool | Info whether there are more historical events to pull. | 
Sample code
List the last ten mentions for the current chat user.
1
Show notifications for mentions
You can monitor all events emitted when you are mentioned in a parent or thread channel you are a member of using the SetListeningForMentionEvents() method. You can use this method to create pop-up notifications for the users.
Events documentation
To read more about the events of type Mention, refer to the Chat events documentation.
Method signature
This method has the following parameters:
1// start listening
2user.SetListeningForMentionEvents(bool listen)
3
4// triggered mention event
5public event Action<MentionEvent> OnMentionEvent;
6// needs a corresponding event handler
7void EventHandler(MentionEvent event)
Input
| Parameter | Description | 
|---|---|
| listen*Type:  boolDefault: n/a | Whether to listen to Usermention events. | 
Output
These methods don't return anything.
Sample code
Print a notification for a mention of the current chat user on the support channel.
1
