Message Reactions API for PubNub Kotlin SDK
Add or remove actions on published messages to build features like receipts, reactions, or to associate custom metadata to messages. Clients can subscribe to a channel to receive message action events on that channel. They can also fetch past message actions from Message Persistence independently or when they fetch original messages.
Calling Kotlin methods
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 choose one of these or the operation will not be performed at all.
For example, the following code is valid and will compile, but the publish won't be performed:
pubnub.publish(
message = "this sdk rules!",
channel = "my_channel"
)
To successfully publish a message, you must follow the actual method invocation with whether to perform it synchronously or asynchronously, for example:
pubnub.publish(
message = "this sdk rules!",
channel = "my_channel"
).async { result, status ->
if (status.error) {
// handle error
} else {
// handle successful method result
}
}
Add Message Reaction
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Description
Add an action on a published message
. Returns the added action in the response.
Method(s)
To Add a Message Reaction you can use the following method(s) in the Kotlin SDK:
pubnub.addMessageAction(
channel: String,
messageAction: PNMessageAction
).async { result, status -> }
Parameter | Type | Required | Description |
---|---|---|---|
channel | String | Yes | Specifies channel name to publish message actions to. |
messageAction | PNMessageAction | Yes | The message action object containing the message action's type, value and the publish timetoken of the original message. See PNMessageAction for more details. |
Basic Usage
pubnub.addMessageAction(
channel = "my_channel",
messageAction = PNMessageAction(
type = "reaction",
value = "smiley_face",
messageTimetoken = 15701761818730000L
)
).async { result, status ->
if (!status.error) {
println(result!!.type)
println(result.value)
println(result.uuid)
println(result.actionTimetoken)
println(result.messageTimetoken)
} else {
show all 19 linesReturns
The addMessageAction()
operation returns a PNAddMessageActionResult
which contains the following operations:
Method | Type | Description |
---|---|---|
type | String | Message action's type. |
value | String | Message action's value. |
uuid | String | Publisher of the message action. |
actionTimetoken | String | Timestamp when the message action was created. |
messageTimetoken | Long | Timestamp when the actual message was created the message action belongs to. |
PNMessageAction
Method | Type | Description |
---|---|---|
type | String | Message action's type. |
value | String | Message action's value. |
messageTimetoken | Long | Timestamp when the actual message was created the message action belongs to. |
Remove Message Reaction
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Description
Remove a previously added action on a published message
. Returns an empty response.
Method(s)
To Remove a Message Reaction you can use the following method(s) in the Kotlin SDK:
pubnub.removeMessageAction(
channel: String,
messageTimetoken: Long,
actionTimetoken: Long
).async { result, status -> }
Parameter | Type | Required | Description |
---|---|---|---|
channel | String | Yes | Specifies channel name to remove message actions from. |
messageTimetoken | Long | Yes | Publish timetoken of the original message. |
actionTimetoken | Long | Yes | Publish timetoken of the message action to be removed. |
Basic Usage
pubnub.removeMessageAction(
channel = "my_channel",
messageTimetoken = 15701761818730000L,
actionTimetoken = 15701775691010000L
).async { _, status ->
if (!status.error) {
// result has no actionable data
// it's enough to check if the status itself is not an error
} else {
// handle error
status.exception?.printStackTrace()
}
}
Returns
The removeMessageAction()
operation returns a no actionable data.
Get Message Reactions
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Description
Get a list of message actions in a channel
. Returns a list of actions sorted by the action's timetoken in ascending order.
Method(s)
To Get Message Reactions you can use the following method(s) in the Kotlin SDK:
pubnub.getMessageActions(
channel: String,
page: PNBoundedPage
)
Parameter | Type | Required | Description |
---|---|---|---|
channel | String | Yes | The channel name. |
page | PNBoundedPage | Optional | The paging object used for pagination. Set limit to specify the number of actions to return in response. Default/Maximum is 100 . start and end denotes the range boundaries. Return values will be less than start and greater or equal to end . |
Basic Usage
pubnub.getMessageActions(
channel = "my_channel"
).async { result, status ->
if (!status.error) {
result!!.actions.forEach {
println(it.type)
println(it.value)
println(it.uuid)
println(it.actionTimetoken)
println(it.messageTimetoken)
}
} else {
// handle error
status.exception?.printStackTrace()
}
show all 16 linesReturns
The getMessageActions()
operation returns a list of PNGetMessageActionsResult?
objects, each containing the following operations:
Method | Type | Description |
---|---|---|
type | String | Message action's type. |
value | String | Message action's value. |
uuid | String | Publisher of the message action. |
actionTimetoken | String | Timestamp when the message action was created. |
messageTimetoken | Long | Timestamp when the actual message was created the message action belongs to. |
page | PNBoundedPage | If exists indicates that more data is available. It can be passed directly to another call of getMessageActions to retrieve this remaining data. |
Other Examples
Fetch Messages with paging
fun main() {
getMessageActionsWithPaging(
channel = "my_channel",
start = System.currentTimeMillis() * 10_000L
) { actions ->
actions.forEach {
println(it.type)
println(it.value)
println(it.uuid)
println(it.messageTimetoken)
println(it.actionTimetoken)
}
}
}
show all 44 lines