let activity = try await feed.addActivity(
request: .init(
text: "Hey @bob and @alice, check this out!",
type: "post",
mentionedUserIds: ["bob", "alice"],
createNotificationActivity: true
)
)
// activity.mentionedUsers contains enriched user objectsUser Mentions
Overview
You can mention users in both activities and comments. When you include mentioned_user_ids, the API stores the mentions and returns enriched mentioned_users in the response. Optionally, you can create notification activities for mentioned users or send push notifications.
Adding user mentions
mentioned_user_ids can only contain existing user ids, you can use queryUsers endpoint to search for users to mention.
Please note that it's up to you to include user mentions (for example @Alice) in the activity/comment text, the Stream API won't modify these fields based on mentioned_user_ids.
Adding an activity with mentions
Adding a comment with mentions
let comment = try await feed.addComment(
request: .init(
comment: "Thanks @bob for the tip!",
objectId: "activity_123",
objectType: "activity",
mentionedUserIds: ["bob"],
createNotificationActivity: true
)
)
// comment.mentionedUsers contains enriched user objectsReading mentioned users
Activities and comments returned by the API include a mentioned_users array with enriched user objects (id, name, image, etc.). Use them to render @mentions in your UI.
// From activities
let feedResponse = try await feed.getOrCreate()
for activity in feedResponse.activities {
if let mentioned = activity.mentionedUsers, !mentioned.isEmpty {
print("Mentioned: \(mentioned.map(\.name).joined(separator: ", "))")
}
}
// From comments (e.g. activity with latest replies)
let activity = try await feed.getActivity(activityId: "activity_123", withReactions: false, withReactionCounts: false, withRecentReplies: 10)
for comment in activity.latestReplies ?? [] {
if let mentioned = comment.mentionedUsers, !mentioned.isEmpty {
print("Comment mentioned: \(mentioned.map(\.name).joined(separator: ", "))")
}
}Mentions in notification feed
- Activities: When
create_notification_activityistrue, each mentioned user receives amentionnotification in their notification feed. - Comments: When
create_notification_activityistrue, each mentioned user receives acomment_mentionnotification (the activity author does not get an extra notification for mentions; they already get a comment notification).
For updating or deleting activities/comments and managing mention notifications (e.g. handle_mention_notifications, delete_notification_activity), see Notification feeds.
Push notifications
Use the skip_push parameter to control if push notifications are sent to mentioned users or not. For more information check the Push notification guide that contains all relevant information about setting up push notification providers, customizing notifications and user preferences.