Mention users
Tag users in chat messages with @ mentions. Type @ followed by at least three letters to see username suggestions.
Generic referencing
Channel references, user mentions, and links are instances of MessageElement 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
- Mentions per message: up to 100 (default: 10)
Implementation follows similar patterns to 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
Add user mentions with @ followed by at least three letters (e.g., @Mar).
Mentioned users are stored in the MessageDraft object. 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() method for details.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
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() removes a 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
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
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 message elements listener returns users matching a 3-letter string from channel members or global users.
Example: typing Sam returns Samantha, Samir, etc. Default limit: 10 users (max: 100).
Method signature
You must add a message elements listener to receive user suggestions.
Refer to the addChangeListener() method for details.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Create a message draft and add a change listener to listen for user mentions.
1
Get mentioned users
getMessageElements() returns all users mentioned in a message.
Method signature
This method has the following signature:
1message.getMessageElements()
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Check if the message contains any mentions.
1
Collect all user-related mentions
getCurrentUserMentions() retrieves all instances where the current user was mentioned in channels or threads. Use this to build a mentions feed.
Method signature
This method has the following signature:
1chat.getCurrentUserMentions(
2 startTimetoken: Timetoken? = nil,
3 endTimetoken: Timetoken? = nil,
4 count: Int = 100
5) async throws -> (mentions: [UserMentionDataWrapper<MessageImpl>], isMore: Bool)
Input
| Parameter | Description |
|---|---|
startTimetokenType: TimetokenDefault: 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. |
endTimetokenType: TimetokenDefault: 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. |
countType: 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 the includeMessageActions parameter in the Swift SDK docs. |
Output
| Parameter | Description |
|---|---|
(mentions: [UserMentionDataWrapper<MessageImpl>], isMore: Bool)Type: object | Returned object containing two fields: mentions and isMore. |
→ enhancedMentionsDataType: enhancedMentionsData (ChannelMentionData or ThreadMentionData) | 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 ChannelMentionData includes these fields: event (of type Event<EventContent.Mention>), channelId where you were mentioned, message that included the mention, userId that mentioned you. For mentions in threads, the returned ThreadMentionData 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. |
→ isMoreType: Bool | Info whether there are more historical events to pull. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
List the last ten mentions for the current chat user.
1
Show notifications for mentions
user.onMentioned() monitors mention events on the current user. Use this to trigger pop-up notifications. You can also use user.stream.mentions() for an AsyncStream-based approach.
Events documentation
To read more about the events of type Mention, refer to the Chat events documentation.
Method signature
1user.onMentioned(
2 callback: @escaping (Mention) -> Void
3) -> AutoCloseable
The Mention struct carries the mention payload:
1public struct Mention {
2 public let messageTimetoken: Timetoken
3 public let channelId: String
4 public let parentChannelId: String?
5 public let mentionedByUserId: String
6}
Input
| Parameter | Description |
|---|---|
callback *Type: (Mention) -> VoidDefault: n/a | Closure called with a Mention whenever the user is mentioned in a message. |
Output
| Parameter | Description |
|---|---|
AutoCloseable | An object you must retain. When released or closed, the listener stops. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
1// Hold a strong reference to the returned "AutoCloseable"; otherwise, it will be canceled.
2let stopMentions = user.onMentioned { mention in
3 debugPrint("Mentioned in channel: \(mention.channelId) by user: \(mention.mentionedByUserId)")
4}
5
6// AsyncStream equivalent
7Task {
8 for await mention in user.stream.mentions() {
9 debugPrint("Mentioned in channel: \(mention.channelId)")
10 }
11}
12
13// To stop listening:
14stopMentions.close()
Get mentioned users (deprecated)
mentionedUsers property returns all users mentioned in a message.
Method signature
This is how you can access the property:
1message.mentionedUsers