Add a Reaction
Users can add a reaction to a message by callingaddReaction with the message ID and the reaction emoji.
- Java
- Kotlin
You can react on Text, Media and Custom messages.
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 aReactionsRequest 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
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
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 theMessageListener: override onMessageReactionAdded() and onMessageReactionRemoved(). Each receives a ReactionEvent, whose getReaction() returns the Reaction and whose getReceiverId(), getReceiverType(), getConversationId() and getParentMessageId() identify where it happened.
- Java
- Kotlin
Removing a Reaction Listener
To stop listening for reaction events, remove the listener with the same listener ID:- 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 it, you would want to update the message with the latest reaction information. To do so, useCometChatHelper.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.
- Java
- Kotlin
message instance’s reactions are updated. You can then use message.getReactions() to get the latest reactions and refresh your UI accordingly.