Chat / iOS
Chat iOS v4
Chat iOS
Chat
iOS
Home
/
Chat
/
iOS
/
Message
This is the new Docs for Chat SDK v4 for iOS. To see the previous Docs, click here.

React to a message in a group channel

Message reactions will 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 acknowledgement of or feelings about a message without written text by simply adding reactions. They can also view and delete their reactions to the message.

Note: Message reactions are currently available only in group channels.

let emojiKey = "smile"
// The BASE_MESSAGE argument below indicates an 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 an BaseMessage object to delete 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.
            }
        }
    }
}

Note: By using the PreviousMessageListQuery instance's loadNextPage(completionHandler:) method, messages along with their reactions can also be retrieved. To learn more, see the page.

If one of the channel members reacts to a message, the channel(_:updatedReaction:) method in the channel event delegate will be invoked on all channel members’ devices including the one that belongs to the current user. The apply(_:) method will reflect 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 reaction made to the message on the current user's chat view.
}