Message reactions help you build a more engaging chat experience that goes beyond text messages. They are a quick and easy way for users to respond to a message. Users can express their feelings about a message by adding reactions instead of typing a response. They can also view and delete their reactions to the message.
Note: Message reactions are supported in Group and Supergroup channels. This feature isn't supported in Open channels and custom message types.
let emojiKey = "smile"
// The BASE_MESSAGE argument below indicates a BaseMessage object to add a reaction to.
channel.addReaction(with: BASE_MESSAGE, key: emojiKey) { event, error in
guard error == nil else {
// Handle error.
return
}
}
// The BASE_MESSAGE argument below indicates a BaseMessage object to remove a reaction from.
channel.deleteReaction(with: BASE_MESSAGE, key: emojiKey) { event, error in
guard error == nil else {
// Handle error.
return
}
}
// To add or remove an emoji reaction made to the message on the current user's chat view,
// the apply() method should be called in the channel event delegate's updatedReaction() method.
You can decide how to display reactions that were added to messages in the current user’s chat view.
let params = MessageListParams()
params.previousResultSize = 10
params.includeReactions = INCLUDE_REACTIONS
channel.getMessagesByTimestamp(TIMESTAMP, params: params) { messages, error in
guard let messages = messages, error == nil else {
// Handle error.
return
}
for message in messages {
for reaction in message.reactions {
// Check if this emoji has been used when the current user reacted to the message.
if reaction.userIds.firstIndex(of: SendbirdChat.getCurrentUser()!.userId) != nil {
let key = reaction.key
let updatedAt = reaction.updatedAt
// Show the emoji however you want on the current user's chat view.
}
}
}
}
By default, each Reaction object's userIds property returns up to ten sampled user IDs. This limit applies to both group and supergroup channels. To change the default limit, contact sales.
To retrieve the full list of users who reacted to a message, call the List reactions Platform API with list_users set to true.
To get the total number of users who reacted with a specific emoji, use the count property of the Reaction object.
// Reactions on the message.
let reactions = message.reactions
// Count of users who reacted with the first emoji in the list, not limited to the sampled user IDs.
let count = reactions[0].count
When one of the channel members reacts to a message, the channel(_:updatedReaction:) method in the channel event delegate is invoked on all channel members’ devices including the one that belongs to the current user. The apply(_:) method reflects the reaction change to the message in real time.
func channel(_ channel: BaseChannel, updatedReaction reactionEvent: ReactionEvent) {
// If there is a message with reactionEvent.messageId,
// you can apply the reaction change to the message by calling the apply() method.
message.apply(reactionEvent)
// Add or remove an emoji below the message on the current user's chat view.
}