Reactions

Add, get, and delete message reactions for messages sent on apps built with the 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(reaction: String): PNFuture<Message>

Input

ParameterTypeRequiredDefaultDescription
reactionStringYesn/aEmoji added to the message or removed from it by the current user.

Output

TypeDescription
PNFuture<Message>Updated message instance with an added reactions action type.

Basic usage

Add the "thumb up" emoji (\u{1F44D}) to the last message on the support channel.

        val channel: Channel
// ...

channel.getHistory(count = 1).async { result ->
result.onSuccess { history ->
history.messages.firstOrNull()?.let { message ->
message.toggleReaction("👍").async {
it.onSuccess { newMessage ->
// success - the returned message needs to replace an existing message in your app’s message list
}.onFailure {
// failure
}
}
}
}.onFailure {
show all 18 lines

Get 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

You can access the reactions property of the Message object to return a list of all reactions added to the given messages (with the ID of the user who added it and the timetoken stating when this action was added).

Basic usage

List all reactions added to the last message on the support channel.

val channel: Channel
// ...

// fetch the last message
channel.getHistory(count = 1).async { result ->
result.onSuccess { historyResult ->
if (historyResult.messages.isNotEmpty()) {
val lastMessage = historyResult.messages.first()
val reactions = lastMessage.reactions
if (reactions.isNotEmpty()) {
reactions.forEach { reaction ->
println("Reaction: ${reaction}")
}
} else {
println("No reactions on the last message.")
show all 23 lines

Get 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 getHistory() method. By default, when you fetch historical messages, PubNub returns all message reactions and metadata attached to the retrieved messages.

Check

hasUserReaction() checks if the current user added a given emoji to the message.

Method signature

This method takes the following parameters:

message.hasUserReaction(reaction: String): Boolean

Input

ParameterTypeRequiredDefaultDescription
reactionStringYesn/aSpecific emoji added to the message.

Output

TypeDescription
BooleanSpecifies if the current user added a given emoji to the message or not.

Basic usage

Check if the current user added the "thumb up" emoji (👍) to the last message on the support channel.

val channel: Channel
// ...

// fetch the last message
channel.getHistory(count = 1).async { result ->
result.onSuccess { historyResult ->
if (historyResult.messages.isNotEmpty()) {
val lastMessage = historyResult.messages.first()
val thumbUpEmoji = "👍"

val hasThumbUpReaction = lastMessage.hasUserReaction(thumbUpEmoji)
if (hasThumbUpReaction) {
println("The current user has added the 'thumb up' emoji to the last message.")
} else {
println("The current user has not added the 'thumb up' emoji to the last message.")
show all 23 lines
Last updated on