---
source_url: https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/users/mentions
title: Mention users
updated_at: 2026-06-26T11:03:55.092Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Mention users

Tag users in chat messages with `@` mentions. Type `@` followed by at least three letters to see username suggestions.

:::tip Generic referencing
Channel references, user mentions, and links are `MessageElement` instances with different `MentionTarget` types.
:::

**Configuration options:**

* **User source**: channel members or all app users
* **Suggestions**: up to 100 usernames (default: 10)
* **Username length**: up to [200 characters](https://www.pubnub.com/docs/sdks/unity/api-reference/objects#set-user-metadata)
* **Mentions per message**: up to 100 (default: 10)

Implementation follows similar patterns to [channel referencing](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/channels/references) and [links](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/links).

:::note Requires App Context
Enable [App Context](https://youtu.be/9UEoSlngpYI) in the [Admin Portal](https://admin.pubnub.com/) to mention users.
:::

## Add user mentions

Add user mentions with `@` followed by at least three letters (e.g., `@Mar`).

Mentioned users are stored in the [MessageDraft object](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/message-draft). When sent with `Send()`, mention data is saved to 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()](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/drafts#add-message-element) 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.

```csharp
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var channelResult = await chat.GetChannel("support");
if (channelResult.Error) return;
var testChannel = channelResult.Result;
        
// Create a message draft
var messageDraft = testChannel.CreateMessageDraft();

// Update the message with the initial text
messageDraft.Update("Hello Alex! I have sent you this link on the #offtopic channel.");

// Add a user mention to the string "Alex"
messageDraft.AddMention(6, 4, new MentionTarget
{
    Target = "alex_d",
    Type = MentionType.User
});
```

## Remove user mentions

`RemoveMention()` lets you remove a previously added user mention from a [draft message](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/drafts).

### 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()](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/drafts#remove-message-element) method for details.

:::warning 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.

```csharp
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
// assume the message reads
// Hello Alex! I have sent you this link on the #offtopic channel.`

// remove the user reference
messageDraft.RemoveMention(6);
```

## Get user suggestions

The event handler you attached to the `OnDraftUpdated` event returns all users referenced in the [draft message](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/message-draft) that match the provided 3-letter string from your app's keyset.

##### Single listener

The event handler returns suggestions for channel references, user mentions, and links using `<List<MessageElement>` and `<List<SuggestedMention>`.

* `<List<MessageElement>` represent the draft's current state and can include plain text or links, such as user mentions, channel references, or URLs. These elements are organized to reflect their order in the draft.
* `<List<SuggestedMention>` provides potential mentions when available.

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 ...`.

:::warning 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](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/drafts#add-message-draft-change-listener) section for details.

### Sample code

Insert the first suggested mention whenever the draft is updated and mentions are detected.

```csharp
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var channelResult = await chat.GetChannel("support");
if (channelResult.Error) return;
var channel = channelResult.Result;
        
var messageDraft = channel.CreateMessageDraft();

messageDraft.ShouldSearchForSuggestions = true;

messageDraft.OnDraftUpdated += (elements, mentions) =>
{
    if (!mentions.Any())
    {
        return;
    }
    messageDraft.InsertSuggestedMention(mentions[0], mentions[0].ReplaceTo);
};

messageDraft.Update("@Alex are you there?");
```

## Get mentioned users

You can access the `MentionedUsers` property of the [Message object](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/message) to return all users mentioned in a message.

### Method signature

This is how you can access the property:

```csharp
message.MentionedUsers
```

#### Properties

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| MentionedUsers | List<User> | Optional |  | List of all users mentioned in a message. |

### Sample code

Check if the message with the `16200000000000000` timetoken contains any mentions.

```csharp
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
// reference the "support" channel
var channelResult = await chat.GetChannel("support");
if (!channelResult.Error)
{
    var channel = channelResult.Result;
    Debug.Log($"Found channel with name {channel.Name}");

    // get the message with the specified timetoken
    var messageResult = await channel.GetMessage("16200000000000000");
    if (!messageResult.Error)
    {
        var message = messageResult.Result;
        Debug.Log($"Message: {message.MessageText}");

        // check if the message contains any mentions
        if (message.MentionedUsers != null && message.MentionedUsers.Count > 0)
        {
            Debug.Log("The message contains mentions.");
            foreach (var mentionedUser in message.MentionedUsers)
            {
                Debug.Log($"Mentioned User: {mentionedUser.Name}");
            }
        }
        else
        {
            Debug.Log("The message does not contain any mentions.");
        }
    }
    else
    {
        Debug.Log("Message with the specified timetoken not found.");
    }
}
else
{
    Debug.Log("Support channel not found.");
}
```

## 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:

```csharp
chat.GetCurrentUserMentions(string startTimeToken, string endTimeToken, int count)
```

#### Input

| Parameter | Description |
| --- | --- |
| `startTimetoken` *Type: `string`Default: n/a | [Timetoken](https://www.pubnub.com/docs/sdks/unity/api-reference/misc#time) delimiting the start of a time slice (exclusive) to pull messages with mentions from. For details, refer to the [Fetch History section](https://www.pubnub.com/docs/sdks/unity/api-reference/storage-and-playback#fetch-history). |
| `endTimetoken` *Type: `string`Default: 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](https://www.pubnub.com/docs/sdks/unity/api-reference/storage-and-playback#fetch-history). |
| `count` *Type: `int`Default: `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 the `IncludeMessageActions` parameter in the [Unity SDK docs](https://www.pubnub.com/docs/sdks/unity/api-reference/storage-and-playback#methods). |

#### Output

This method returns an awaitable `Task<UserMentionsWrapper>` with the wrapper object containing two fields: `Mentions` and `IsMore`.

| Parameter | Description |
| --- | --- |
| `Mentions`Type: `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 `UserMentionData` includes these fields: `ChannelId` where you were mentioned, [userId](https://www.pubnub.com/docs/general/setup/users-and-devices) that mentioned you, `Event` (of type `Mention`), `Message` that included the mention. For mentions in threads, the returned `UserMentionData` includes similar fields, the only difference is that you'll get `ParentChannelId` and `ThreadChannelId` fields instead of just `ChannelId` to clearly differentiate the thread that included the mention from the parent channel in which this thread was created. |
| `IsMore`Type: `bool` | Info whether there are more historical events to pull. |

### Sample code

List the last ten mentions for the current chat user.

```csharp
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
// fetch the last 10 mentions for the current user
var mentions = await chat.GetCurrentUserMentions(string.Empty, string.Empty, 10);

if (!mentions.Error && mentions.Result.Mentions.Any())
{
    foreach (var mention in mentions.Result.Mentions)
    {
        Debug.Log($"Mentioned in Channel ID: {mention.ChannelId}, Message: {mention.Message.MessageText}");
    }
}
else
{
    Debug.Log("No mentions found.");
}
```

## 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.

Use the `StreamMentionEvents()` method to enable or disable mention event streaming on a user, and the `OnMentioned` event to handle mention updates. You can use this to create pop-up notifications for the users.

:::tip Events documentation
To read more about the events of type `Mention`, refer to the [Chat events](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/custom-events#chat-events) documentation.
:::

:::tip Method naming
Earlier versions used `SetListeningForMentionEvents()` to enable streaming. This method has been superseded by `StreamMentionEvents()`, though it remains available for backward compatibility.
:::

### Method signature

These methods take the following parameters:

* StreamMentionEvents() 1user.StreamMentionEvents(bool stream)
* OnMentioned - Event signature 1// event on the User entity — enabled by StreamMentionEvents()2public event Action<Mention> OnMentioned;3// needs a corresponding event handler4void EventHandler(Mention mention)

#### Input

| Parameter | Required in `StreamMentionEvents()` | Required in `OnMentioned` | Description |
| --- | --- | --- | --- |
| `stream`Type: `bool`Default: n/a | Yes | n/a | Whether to start (`true`) or stop (`false`) listening to mention events for the user. |
| `mention`Type: [Mention](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/event#typed-event-data)Default: n/a | No | Yes | Typed mention data containing the message text, timetoken, channel ID, optional parent channel (for threads), and the mentioner's user ID. |

#### Output

These methods don't return a value. Mention event updates are delivered through the `OnMentioned` event handler.

### Sample code

Print a notification for a mention of the current chat user on the `support` channel.

```csharp
using System.Linq;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;
// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var userResult = await chat.GetCurrentUser();
if (userResult.Error)
{
    return;
}
var user = userResult.Result;
user.StreamMentionEvents(true);
user.OnMentioned += mentionEvent => 
{
    if(mentionEvent.ChannelId == "support")
    {
        Debug.Log($"{user.Id} has been mentioned on the support channel!");
    }
};
```