Reactions
Add, get, and delete message reactions for messages sent on apps built with the Unity Chat SDK.
Add & delete
ToggleReaction()
is a method for both adding and removing message reactions. It adds a string flag to the message if the current user hasn't added it yet or removes it if the current user already added it before.
If you use this method to add or remove message reactions, this flag would be a literal emoji you could implement in your app's UI. However, you could also use this method for a different purpose, like marking a message as pinned to a channel or unpinned if you implement the pinning feature in your chat app.
Method signature
This method takes the following parameters:
message.ToggleReaction(string reactionValue)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
reactionValue | string | Yes | n/a | Emoji added to the message or removed from it by the current user. |
Output
This method doesn't return any data.
Basic usage
Add the "thumb up" emoji (\u{1F44D}
) to the last message on the support
channel.
// reference the "support" channel and ensure it's found
if (chat.TryGetChannel("support", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");
// get the message history with the desired count
var messageHistory = channel.GetMessageHistory(null, null, 1);
// get the last message from the returned list
var lastMessage = messageHistory.Messages.FirstOrDefault();
if (lastMessage != null)
{
// add the "thumb up" emoji to the last message
lastMessage.ToggleReaction("\u{1F44D}");
show all 26 linesGet updates
To learn how to receive updates whenever a message reaction is added, edited, or removed on other clients, head to the Get updates section.
Get reactions for one message
Reactions()
is a method that returns a list of all reactions added to the given message.
Method signature
This method has the following signature:
message.Reactions
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
List<MessageAction> | A list of MessageAction objects, each containing details of a reaction including its type, value, timestamp, and the user ID of the person who added the reaction. |
Basic usage
List all reactions added to the last message on the support
channel.
// reference the "support" channel and ensure it's found
if (chat.TryGetChannel("support", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");
// get the message history with the desired count
var messageHistory = channel.GetMessageHistory(null, null, 1);
// get the last message from the returned list
var lastMessage = messageHistory.Messages.FirstOrDefault();
if (lastMessage != null)
{
// output all reactions added to the last message
var reactions = lastMessage.Reactions;
show all 29 linesGet historical reactions
If you have Message Persistence enabled on your keyset, PubNub stores all historical info about messages, their metadata, and reactions.
If you want to fetch historical info about message reactions, use the GetMessageHistory()
method. By default, when you fetch historical messages, PubNub returns all message reactions and metadata attached to the retrieved messages.
Check reactions
HasUserReaction()
checks if the current user added a given emoji to the message.
Method signature
This method takes the following parameters:
message.HasUserReaction(string reactionValue)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
reactionValue | string | Yes | n/a | Specific emoji added to the message. |
Output
Type | Description |
---|---|
bool | Specifies if the message has the specified user reaction (true ) or not (false ). |
Basic usage
Check if the current user added the "thumb up" emoji (👍) to the last message on the support
channel.
// reference the "support" channel and ensure it's found
if (chat.TryGetChannel("support", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");
// get the message history with the desired count
var messageHistory = channel.GetMessageHistory(null, null, 1);
// get the last message from the returned list
var lastMessage = messageHistory.Messages.FirstOrDefault();
if (lastMessage != null)
{
// Check if the current user added the "thumb up" emoji to the last message
if (lastMessage.HasUserReaction("\u{1F44D}"))
show all 32 lines