Reactions let users respond to messages with emoji. You can add or remove reactions, fetch all reactions on a message, listen for reaction events in real time, and update your UI when reactions change.Documentation Index
Fetch the complete documentation index at: https://cometchat-22654f5b-docs-android-v6-beta2.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Add a Reaction
Users can add a reaction to a message by callingaddReaction with the message ID and the reaction emoji. On success, returns a BaseMessage object with updated reactions.
- Java
- Kotlin
You can react on Text, Media and Custom messages. For more details on message types, see Message Structure & Hierarchy.
Remove a Reaction
Removing a reaction from a message can be done using theremoveReaction method.
- Java
- Kotlin
Fetch Reactions for a Message
To get all reactions for a specific message, first create aReactionRequest using ReactionRequestBuilder. You can specify the number of reactions to fetch with setLimit with max limit 20. For this, you will require the ID of the message. This ID needs to be passed to the setMessageId() method of the builder class. The setReaction() will allow you to fetch details for specific reaction or emoji.
| Setting | Description |
|---|---|
setMessageId(int value) | Specifies the unique identifier of the message for which you want to fetch reactions. This parameter is mandatory as it tells the SDK which message’s reactions are being requested. |
setReaction(String value) | Filters the reactions fetched by the specified reaction type (e.g., ”😊”, ”😂”, ”👍”). When set, this method will cause the ReactionRequest to only retrieve details of the provided reaction for the given message. |
Fetch Next
ThefetchNext() method fetches the next set of reactions for the message.
- Java
- Kotlin
Fetch Previous
ThefetchPrevious() method fetches the previous set of reactions for the message.
- Java
- Kotlin
Real-time Reaction Events
Keep the chat interactive with real-time updates for reactions. Register a listener for these events and make your UI responsive. For more information on listeners, see Real-Time Listeners.- Java
- Kotlin
Removing a Reaction Listener
To stop listening for reaction events, remove the listener as follows:- Java
- Kotlin
Get Reactions List
To retrieve the list of reactions reacted on particular message, you can use themessage.getReactions() method. This method will return an array containing the reactions, or an empty array if no one reacted on the message.
- Java
- Kotlin
Check if Logged-in User has Reacted on Message
To check if the logged-in user has reacted on a particular message or not, You can use thegetReactedByMe() method on any ReactionCount object instance. This method will return a boolean value, true if the logged-in user has reacted on that message, otherwise false.
- Java
- Kotlin
Update Message With Reaction Info
When a user adds or removes a reaction, you will receive a real-time event. Once you receive the real time event you would want to update the message with the latest reaction information. To do so you can use theupdateMessageWithReactionInfo() method.
The updateMessageWithReactionInfo() method provides a seamless way to update the reactions on a message instance (BaseMessage) in real-time. This method ensures that when a reaction is added or removed from a message, the BaseMessage object’s getReactions() property reflects this change immediately.
When you receive a real-time reaction event (ReactionEvent), call the updateMessageWithReactionInfo() method, passing the BaseMessage instance (message), the inner Reaction from the event (for example reactionEvent.getReaction()), and the reaction event action type (CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVED) that corresponds to the message being reacted to.
- Java
- Kotlin
message instance’s reactions are updated. You can then use message.getReactions() to get the latest reactions and refresh your UI accordingly.
Reaction Payload Structure
Reaction Object
Reaction Object
When fetching reactions for a message, each
Sample Reaction Object:
Reaction object contains the following fields:| Parameter | Type | Description |
|---|---|---|
id | String | Unique reaction identifier |
messageId | long | ID of the message that was reacted to |
reaction | String | The reaction emoji |
uid | String | UID of the user who reacted |
reactedAt | long | Unix timestamp when reaction was added |
reactedBy | User | User who added the reaction |
User Object (Reactions)
User Object (Reactions)
The nested
User object in reactedBy contains:| Parameter | Type | Description |
|---|---|---|
uid | String | Unique identifier of the user |
name | String | Display name of the user |
avatar | String | URL to user’s profile picture |
link | String | URL to user’s profile page |
role | String | User role for access control |
metadata | JSONObject | Custom data set by developer |
status | String | User online status. Values: "online", "offline" |
statusMessage | String | Custom status message |
lastActiveAt | long | Unix timestamp of last activity |
hasBlockedMe | boolean | Whether this user has blocked the logged-in user |
blockedByMe | boolean | Whether the logged-in user has blocked this user |
tags | Array<String> | List of tags for user identification |
deactivatedAt | long | Unix timestamp when user was deactivated (0 if active) |
ReactionCount Object
ReactionCount Object
When accessing reactions on a message via
Sample ReactionCount Object:
message.getReactions(), each ReactionCount object contains:| Parameter | Type | Description |
|---|---|---|
reaction | String | The reaction emoji |
count | int | Total count of this reaction |
reactedByMe | boolean | Whether the logged-in user has reacted with this emoji |
Next Steps
Send Messages
Learn how to send text, media, and custom messages
Receive Messages
Handle real-time message events with listeners
Threaded Messages
Organize conversations with message threads
Mentions
Tag users in messages with @mentions