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

React to a message in a group channel

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

val emojiKey = "smile"
// The BASE_MESSAGE argument below indicates a BaseMessage object to add a reaction to.
groupChannel.addReaction(BASE_MESSAGE, emojiKey) { reactionEvent, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

// The BASE_MESSAGE argument below indicates a BaseMessage object to delete a reaction from.
groupChannel.deleteReaction(BASE_MESSAGE, emojiKey) { reactionEvent, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

// To add or remove an emoji reaction made to the message on the current user's chat view,
// the applyReactionEvent() method should be called in the channel event handler's onReactionUpdated() method.

You can decide how to display reactions that were added to messages in the current user’s chat view.

val params = MessageListParams().apply {
    inclusive = INCLUSIVE
    previousResultSize = PREVIOUS_RESULT_SIZE
    nextResultSize = NEXT_RESULT_SIZE
    reverse = REVERSE
    messagePayloadFilter = MessagePayloadFilter().apply {
        includeReactions = INCLUDE_REACTIONS
    }
    // ...
}

groupChannel.getMessagesByTimestamp(TIMESTAMP, params) { messages, e ->
    if (e != null) {
        // Handle error.
    }

    messages.forEach { message ->
        // ...
        message.reactions.forEach { reaction ->
            val userIds = reaction.userIds

            // Check if this emoji has been used when the current user reacted to the message.
            if (userIds.contains(SendbirdChat.currentUser?.userId)) {
                val key = reaction.key
                val updatedAt = reaction.updatedAt

                // Show the emoji however you want on the current user's chat view.
            }
        }
    }
}

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

When one of the channel members reacts to a message, the onReactionUpdated() method in the channel event handler is invoked on all channel members’ devices including the one that belongs to the current user. The applyReactionEvent() method reflects the reaction change to the message in real time.

SendbirdChat.addChannelHandler(
    UNIQUE_HANDLER_ID,
    // ...
    object : GroupChannelHandler() {
        override fun onReactionUpdated(channel: BaseChannel, reactionEvent: ReactionEvent) {
            // ...

            // If there is a message with reactionEvent.getMessageId(),
            // you can apply the reaction change to the message by calling the applyReactionEvent() method.
            message.applyReactionEvent(reactionEvent)

            // Add or remove an emoji below the message on the current user's chat view.
        }
    }
)