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,
completion: ((Swift.Result<MessageImpl, Error>) -> Void)? = nil
)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
reaction | String | Yes | n/a | Emoji added to the message or removed from it by the current user. |
Output
Type | Description |
---|---|
MessageImpl | 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.
// Assuming you have all necessary dependencies and a "chat" instance available
// Fetch the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let supportChannel = channel {
print("Fetched support channel metadata with ID: \(supportChannel.id)")
// Get the latest message from the "support" channel's history
supportChannel.getHistory(count: 1) { historyResult in
switch historyResult {
case let .success(response):
if let latestMessage = response.messages.first {
print("Latest message: \(latestMessage)")
// Add the "thumb up" emoji reaction to the latest message
latestMessage.toggleReaction(reaction: "\u{1F44D}") { reactionResult in
show all 36 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
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.
// Assuming you have all necessary dependencies and a "chat" instance available
// Fetch the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let supportChannel = channel {
print("Fetched support channel metadata with ID: \(supportChannel.id)")
// Get the latest message from the "support" channel's history
supportChannel.getHistory(count: 1) { historyResult in
switch historyResult {
case let .success(response):
if let latestMessage = response.messages.first {
print("Latest message: \(latestMessage)")
// List all reactions to the latest message
show all 34 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 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) -> Bool
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
reaction | String | Yes | n/a | Specific emoji added to the message. |
Output
Type | Description |
---|---|
Bool | Specifies 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.
// Assuming you have all necessary dependencies and a "chat" instance available
// Fetch the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let supportChannel = channel {
print("Fetched support channel metadata with ID: \(supportChannel.id)")
// Get the latest message from the "support" channel's history
supportChannel.getHistory(count: 1) { historyResult in
switch historyResult {
case let .success(response):
if let latestMessage = response.messages.first {
print("Latest message: \(latestMessage)")
// Check if the current user added the "thumb up" emoji reaction
show all 35 lines