/ SDKs / Unity
SDKs
Chat SDKs Unity v4
Chat SDKs Unity
Chat SDKs
Unity
Version 4

React to a message in a group channel

Copy link

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.

// The BASE_MESSAGE argument below indicates a BaseMessage object to add a reaction to.
groupChannel.AddReaction(BASE_MESSAGE, inKey: "smile", (inReactionEvent, inError) =>
{
    if (inError != null)
    {
        return; // Handle error.
    }
});

// The BASE_MESSAGE argument below indicates a BaseMessage object to remove a reaction from.
groupChannel.DeleteReaction(BASE_MESSAGE, inKey: "smile", (inReactionEvent, inError) =>
{
    if (inError != null)
    {
        return; // Handle error.
    }
});

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

SbMessageListParams messageListParams = new SbMessageListParams();
messageListParams.IncludeReactions = true;

groupChannel.GetMessagesByTimestamp(TIMESTAMP, messageListParams, (inMessages, inError) =>
{
    if (inError != null)
    {
        return; // Handle error.
    }

    foreach (SbBaseMessage message in inMessages)
    {
        foreach (SbReaction reaction in message.Reactions)
        {
            // Check if this emoji has been used when the current user reacted to the message.
            if (reaction.UserIds.Contains(SendbirdChat.CurrentUser.UserId))
            {
                string key = reaction.Key;
                long updateAt = reaction.UpdatedAt;
                // Show the emoji however you want on the current user's chat view.
            }
        }
    }
});

Note: By using the SbPreviousMessageListQuery 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.

SbGroupChannelHandler channelHandler = new SbGroupChannelHandler
{
    OnReactionUpdated = (inChannel, inReactionEvent) =>
    {
        // If there is a message with the inReactionEvent.MessageId, you can apply 
        // the reaction change to the message by calling the 
        // message.ApplyReactionEvent(inReactionEvent) method. 
        // Add or remove an emoji that appears below the message on the current user's chat view.
        message.ApplyReactionEvent(inReactionEvent);
    }
};

SendbirdChat.GroupChannel.AddGroupChannelHandler(UNIQUE_HANDLER_ID, channelHandler);