Message Actions API for Kotlin SDK

Breaking changes in v9.0.0

PubNub Kotlin SDK version 9.0.0 unifies the codebases for Kotlin and Java SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0 ) of the Kotlin SDK.

For more details about what has changed, refer to Java/Kotlin SDK migration guide.

Use message actions to add or remove metadata on published messages. Common uses include receipts and reactions. Clients subscribe to a channel to receive message action events. Clients can also fetch past message actions from Message Persistence, either on demand or when fetching original messages.

Request execution

Most PubNub Kotlin SDK method invocations return an Endpoint object, which allows you to decide whether to perform the operation synchronously or asynchronously.

You must invoke the .sync() or .async() method on the Endpoint to execute the request, or the operation will not be performed.

val channel = pubnub.channel("channelName")

channel.publish("This SDK rules!").async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}
Reactions

"Message Reactions" is a specific application of the Message Actions API for emoji or social reactions.

Message Actions vs. Message Reactions

Message Actions is the flexible, low-level API for adding any metadata to messages (read receipts, delivery confirmations, custom data), while Message Reactions specifically refers to using Message Actions for emoji/social reactions.

In PubNub Core and Chat SDKs, the same underlying Message Actions API is referred to as Message Reactions when used for emoji reactions - it's the same functionality, just different terminology depending on the use case.

Add message action

Requires Message Persistence

Enable Message Persistence for your key in the Admin Portal as described in the support article.

Add an action to a published message. The response includes the added action.

Method(s)

Use this Kotlin method:

pubnub.addMessageAction(
channel: String,
messageAction: PNMessageAction
).async { result -> }
* required
ParameterDescription
channel *
Type: String
Channel name to add the message action to.
messageAction *Message action payload (type, value, message timetoken).

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Returns

The addMessageAction() operation returns a PNAddMessageActionResult:

MethodDescription
type
Type: String
Message action type.
value
Type: String
Message action value.
uuid
Type: String
Publisher of the message action.
actionTimetoken
Type: String
Timestamp when the message action was created.
messageTimetoken
Type: Long
Timestamp when the message was created that the action belongs to.

PNMessageAction

MethodDescription
type
Type: String
Message action type.
value
Type: String
Message action value.
messageTimetoken
Type: Long
Timetoken of the target message.

Remove message action

Requires Message Persistence

Enable Message Persistence for your key in the Admin Portal as described in the support article.

Remove a previously added action from a published message. The response is empty.

Method(s)

Use this Kotlin method:

pubnub.removeMessageAction(
channel: String,
messageTimetoken: Long,
actionTimetoken: Long
).async { result -> }
* required
ParameterDescription
channel *
Type: String
Channel name to remove the message action from.
messageTimetoken *
Type: Long
Timetoken of the target message.
actionTimetoken *
Type: Long
Timetoken of the message action to remove.

Sample code


Returns

The removeMessageAction() operation returns no actionable data.

Get message actions

Requires Message Persistence

Enable Message Persistence for your key in the Admin Portal as described in the support article.

Get a list of message actions in a channel. The response sorts actions by the action timetoken in ascending order.

Method(s)

Use this Kotlin method:

pubnub.getMessageActions(
channel: String,
page: PNBoundedPage
)
* required
ParameterDescription
channel *
Type: String
Channel name to list message actions for.
page
Type: PNBoundedPage
Paging object. Set limit to specify the number of actions to return (default/maximum is 100). Use start and end to set range boundaries (results are < start and ≥ end).

Sample code


Returns

The getMessageActions() operation returns a list of PNGetMessageActionsResult? objects:

MethodDescription
type
Type: String
Message action type.
value
Type: String
Message action value.
uuid
Type: String
Publisher of the message action.
actionTimetoken
Type: String
Timestamp when the message action was created.
messageTimetoken
Type: Long
Timestamp when the message was created that the action belongs to.
page
Type: PNBoundedPage
If present, more data is available. Pass it to another getMessageActions call to retrieve remaining data.

Other examples

Fetch messages with paging


Last updated on