Skip to main content
Enhance user engagement in your chat application with message reactions. Users can express their emotions using reactions to messages. This feature allows users to add or remove reactions, and to fetch all reactions on a message. You can also listen to reaction events in real-time. Let’s see how to work with reactions in CometChat’s Android SDK.

Add a Reaction

Users can add a reaction to a message by calling addReaction with the message ID and the reaction emoji.
You can react on Text, Media and Custom messages.

Remove a Reaction

Removing a reaction from a message can be done using the removeReaction method.

Fetch Reactions for a Message

To get all reactions for a specific message, first create a ReactionsRequest using ReactionsRequestBuilder. You can specify the number of reactions to fetch with setLimit (default 10, maximum 1000). 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.

Fetch Next

The fetchNext() method fetches the next set of reactions for the message.
Starting with v5.1.0, the CallbackListener overload of `fetchNext()` / `fetchPrevious()` is deprecated. Use `ReactionFetchListener` instead; it requires SDK 5.1.0 or later, so on 5.0.x keep using CallbackListener. With offline support off, it behaves exactly like the old callback: onError is unchanged, and the server result arrives in the onServer… callback instead of onSuccess. With offline support on, it also delivers the cached result first; the old overload keeps working but receives only the server result. See Show Cached Data.

Fetch Previous

The fetchPrevious() method fetches the previous set of reactions for the message.

Offline Behaviour

With offline support enabled, ReactionsRequest.fetchNext() and fetchPrevious() on a ReactionFetchListener first deliver onCachedReactions from the device (empty when nothing was recorded), then onServerReactions with the live page to reconcile by reaction id. A request object caches only the first direction it fetches, so use a separate ReactionsRequest for each direction. Offline, a read ends with onError with code ERROR_INTERNET_UNAVAILABLE after the cached page. See Show Cached Data. The cached phase replays the page this request last received from the server online, in the same direction; it does not re-run these settings as a query against the cache.

Real-time Reaction Events

Keep the chat interactive with real-time updates for reactions. Reaction events arrive on the MessageListener: override onMessageReactionAdded() and onMessageReactionRemoved(). Each receives a ReactionEvent, whose getReaction() returns the Reaction and whose getReceiverId(), getReceiverType(), getConversationId() and getParentMessageId() identify where it happened.

Removing a Reaction Listener

To stop listening for reaction events, remove the listener with the same listener ID:

Get Reactions List

To retrieve the list of reactions reacted on particular message, you can use the message.getReactions() method. This method will return an array containing the reactions, or an empty array if no one reacted on the message.

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 the getReactedByMe() 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.

Update Message With Reaction Info

When a user adds or removes a reaction, you will receive a real-time event. Once you receive it, you would want to update the message with the latest reaction information. To do so, use CometChatHelper.updateMessageWithReactionInfo(). Pass the BaseMessage the reaction belongs to, the Reaction from the event (reactionEvent.getReaction()), and the action: CometChatConstants.REACTION_ADDED from onMessageReactionAdded() or CometChatConstants.REACTION_REMOVED from onMessageReactionRemoved(). It returns the message with its getReactions() updated.
After calling this method, the message instance’s reactions are updated. You can then use message.getReactions() to get the latest reactions and refresh your UI accordingly.