Emojis are widely used in real-time chats because they allow users to express their feelings in a direct and vivid way. In Agora Chat, "reactions" allow users to quickly react to a message using emojis in one-to-one chats and chat groups. In group chats, reactions can also be used to cast a vote, for example, by calculating the number of different emojis attached to the message.
The following illustration shows the implementation of adding a reaction to a message, how a conversation looks with reactions, and what retrieving a list of reactions (with related information) looks like.
This page shows how to use the Agora Chat SDK to implement reactions in your project.
The SDK provides the following APIs to implement reaction functionalities:
AddReaction
: Adds a reaction to the specified message.RemoveReaction
: Removes the reaction from the specified message.GetReactionList
: Retrieves a list of reactions from the server.GetReactionDetail
: Retrieves the details of the reaction from the server.Message.ReactionList
: Retrieves a list of reactions from the Message object.Before proceeding, ensure that your environment meets the following requirements:
This section introduces how to implement reaction functionalities in your project.
Call AddReaction
to add a reaction to the specified message. You can use MessageReactionDidChange
to listen for the state of adding the reaction.
// Adds a reaction to the specified message
SDKClient.Instance.ChatManager.AddReaction(msg_id, reaction, new CallBack(
onSuccess: () =>
{
Debug.Log($"AddReaction success.");
},
onError: (code, desc) =>
{
Debug.Log($"AddReaction failed, code:{code}, desc:{desc}");
}
));
// Listens for the reaction changes.
class ReactionManagerDelegate : IReactionManagerDelegate
{
public void MessageReactionDidChange(List<MessageReactionChange> list)
{
if (list.Count == 0) return;
foreach(var it in list)
{
// Add the iteration logic here.
}
}
}
// Adds a listener for the reaction.
ReactionManagerDelegate reactionManagerDelegate = new ReactionManagerDelegate();
SDKClient.Instance.ChatManager.AddReactionManagerDelegate(reactionManagerDelegate);
Call RemoveReaction
to remove the specified reaction. You can also listen for the reaction change in MessageReactionDidChange
.
// Removes the reaction.
SDKClient.Instance.ChatManager.RemoveReaction(msg_id, reaction, new CallBack(
onSuccess: () =>
{
Debug.Log($"RemoveReaction success.");
},
onError: (code, desc) =>
{
Debug.Log($"RemoveReaction failed, code:{code}, desc:{desc}");
}
));
// Listens for the reaction changes.
class ReactionManagerDelegate : IReactionManagerDelegate
{
public void MessageReactionDidChange(List<MessageReactionChange> list)
{
if (list.Count == 0) return;
foreach(var it in list)
{
// Add the iteration logic here.
}
}
}
// Adds a listener for the reaction.
ReactionManagerDelegate reactionManagerDelegate = new ReactionManagerDelegate();
SDKClient.Instance.ChatManager.AddReactionManagerDelegate(reactionManagerDelegate);
Call GetReactionList
to retrieve a list of reactions from the server. This method also returns the basic information of the reactions, including the content of the reaction, the number of users that added or removed the reaction, and a list of the first three user IDs that added or removed the reaction.
SDKClient.Instance.ChatManager.GetReactionList(messageIdList, chatType, groupId, new ValueCallBack<Dictionary<string, List<MessageReaction>>>(
onSuccess: (dict) =>
{
// Iterates through the returned reaction dictionary.
foreach (var it in dict)
{
// Iterates each list of reaction in the dictionary.
List<MessageReaction> rl = it.Value;
foreach (var lit in rl)
{
// Handles each reaction.
}
}
},
onError: (code, desc) =>
{
Debug.Log($"GetReactionList failed, code:{code}, desc:{desc}");
}
));
Call GetReactionDetail
to get the detailed information of the reaction from the server. The detailed information includes the reaction content, the number of users that added or removed the reaction, and the complete list of user IDs that added or removed the reaction.
SDKClient.Instance.ChatManager.GetReactionDetail(msg_id, reaction, cursor, pageSize, new ValueCallBack<CursorResult<MessageReaction>>(
onSuccess: (ret) =>
{
Debug.Log($"GetReactionDetail success");
if (ret.Data.Count > 0)
{
MessageReaction mr = ret.Data[0];
// Handles the retrieved reaction.
}
},
onError: (code, desc) =>
{
Debug($"GetReactionDetail failed, code:{code}, desc:{desc}");
}
));