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.
To get all reactions for a specific message, first create a ReactionRequest using ReactionRequestBuilder. You can specify the number of reactions to fetch with setLimit with max limit 100. 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.
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
Report incorrect code
Copy
Ask AI
private String listenerID = "UNIQUE_LISTENER_ID";CometChat.addMessageListener(listenerID, new CometChat.MessageReactionListener() { @Override public void onMessageReactionAdded(MessageReaction reaction) { Log.e(TAG, "Reaction added"); } @Override public void onMessageReactionRemoved(MessageReaction reaction) { Log.e(TAG, "Reaction removed"); }});
Report incorrect code
Copy
Ask AI
val listenerID = "UNIQUE_LISTENER_ID"CometChat.addMessageListener(listenerID, object : CometChat.MessageReactionListener { override fun onMessageReactionAdded(reaction: MessageReaction?) { Log.e(TAG, "Reaction added") } override fun onMessageReactionRemoved(reaction: MessageReaction?) { Log.e(TAG, "Reaction removed") }})
val listenerID = "UNIQUE_LISTENER_ID"CometChat.removeMessageListener(listenerID)
Always remove listeners when they’re no longer needed (e.g., in onDestroy() or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling.
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.
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.
Java
Kotlin
Report incorrect code
Copy
Ask AI
for (ReactionCount reactionCount : message.getReactions()) { Log.e(TAG, "isReactedByMe" + reactionCount.getReactedByMe()); //Return true is logged-in user reacted on that message, otherwise false}
Report incorrect code
Copy
Ask AI
for (reactionCount in message.reactions) { Log.e(TAG, "isReactedByMe" + reactionCount.reactedByMe) //Return true is logged-in user reacted on that message, otherwise false}
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 the updateMessageWithReactionInfo() 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 (MessageReaction), call the updateMessageWithReactionInfo() method, passing the BaseMessage instance (message), event data (MessageReaction) and reaction event action type (CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVED) that corresponds to the message being reacted to.
Java
Kotlin
Report incorrect code
Copy
Ask AI
// The message to which the reaction is relatedBaseMessage message = ...;// The reaction event data received in real-timeMessageReaction messageReaction = ...;// The recieved reaction event real-time action type. Can be CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVEDString action = CometChatConstants.REACTION_ADDED;BaseMessage modifiedBaseMessage = CometChatHelper.updateMessageWithReactionInfo(baseMessage, messageReaction, action);
Report incorrect code
Copy
Ask AI
// The message to which the reaction is relatedval message: BaseMessage = ...// The reaction event data received in real-timeval messageReaction: MessageReaction = ...// The recieved reaction event real-time action type. Can be CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVEDval action = CometChatConstants.REACTION_ADDED// Update the BaseMessage instance with the new reaction informationval modifiedBaseMessage: BaseMessage = CometChatHelper.updateMessageWithReactionInfo( message, messageReaction, action)
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.
Define a specific set of allowed emoji reactions in your app to maintain consistency and prevent inappropriate reactions. Consider using a reaction picker UI component with predefined options.
Update UI Optimistically
Update your UI immediately when a user adds or removes a reaction, then handle the server response. This provides instant feedback and improves perceived performance.
Use updateMessageWithReactionInfo()
Always use CometChatHelper.updateMessageWithReactionInfo() when receiving real-time reaction events to keep your message objects synchronized with the latest reaction data.
Handle Reaction Limits
Consider implementing limits on the number of reactions per message or per user to prevent spam and maintain a clean UI.