Message Reactions API for PubNub Java SDK
Breaking changes in v9.0.0
PubNub Java SDK version 9.0.0 unifies the codebases for Java and Kotlin 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 Java SDK.
For more details about what has changed, refer to Java/Kotlin SDK migration guide.
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.
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.
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 Java SDK:
this.pubnub.addMessageAction()
.channel(String)
.messageAction(PNMessageAction);
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 . |
async | Consumer<Result> | Yes | Consumer of a Result of type PNAddMessageActionResult . |
Basic Usage
pubnub.addMessageAction()
.channel("my_channel")
.messageAction(new PNMessageAction()
.setType("reaction")
.setValue("smiley_face")
.setMessageTimetoken(15701761818730000L)
)
.async(result -> {
result.onSuccess(res -> {
System.out.println(res.getType());
System.out.println(res.getValue());
System.out.println(res.getUuid());
System.out.println(res.getActionTimetoken());
System.out.println(res.getMessageTimetoken());
}).onFailure(exception -> {
show all 18 linesReturns
The addMessageAction()
operation returns a PNAddMessageActionResult
which contains the following operations:
Method | Type | Description |
---|---|---|
getType() | String | Message action's type. |
getValue() | String | Message action's value. |
getUuid() | String | Publisher of the message action. |
getActionTimetoken() | Long | Timestamp when the message action was created. |
getMessageTimetoken() | Long | Timestamp when the actual message was created the message action belongs to. |
PNMessageAction
Method | Type | Description |
---|---|---|
setType() | String | Message action's type. |
setValue() | String | Message action's value. |
setMessageTimetoken() | Long | Timestamp of 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.
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 Java SDK:
this.pubnub.removeMessageAction()
.channel(String)
.messageTimetoken(Long)
.actionTimetoken(Long);
Parameter | Type | Required | Description |
---|---|---|---|
channel | String | Yes | Specifies channel name to publish message actions to. |
messageTimetoken | Long | Yes | Publish timetoken of the original message |
actionTimetoken | Long | Yes | Publish timetoken of the message action to be removed. |
async | Consumer<Result> | Yes | Consumer of a Result of type PNRemoveMessageActionResult . |
Basic Usage
pubnub.removeMessageAction()
.channel("my_channel")
.messageTimetoken(15701761818730000L)
.actionTimetoken(15701775691010000L)
.async(result -> { /* check result */ });
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.
Get a list of message actions in a channel
. Returns a list of actions sorted by the action's timetoken in ascending order.
Truncated response
Number of message actions in the response may be truncated when internal limits are hit. If the response is truncated, a more
property will be returned with additional parameters. Send iterative calls to Message Persistence adjusting the parameters to fetch more message actions.
Method(s)
To Get Message Reactions you can use the following method(s) in the Java SDK:
this.pubnub.getMessageActions()
.channel(String)
.start(Long)
.end(Long)
.limit(Integer);
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channel | String | Yes | The channel name. | |
start | Long | Optional | Message Reaction timetoken denoting the start of the range requested (return values will be less than start ). | |
end | Long | Optional | Message Reaction timetoken denoting the end of the range requested (return values will be greater than or equal to end ). | |
limit | Integer | Optional | 100 | Specifies the number of actions to return in response. Default/Maximum is 100 . |
async | Consumer<Result> | Yes | Consumer of a Result of type PNGetMessageActionsResult . |
Basic Usage
pubnub.getMessageActions()
.channel("my_channel")
.async(result -> {
result.onSuccess(res -> {
List<PNMessageAction> actions = res.getActions();
for (PNMessageAction action : actions) {
System.out.println(action.getType());
System.out.println(action.getValue());
System.out.println(action.getUuid());
System.out.println(action.getActionTimetoken());
System.out.println(action.getMessageTimetoken());
}
}).onFailure(exception -> {
exception.printStackTrace();
});
show all 16 linesReturns
The getMessageActions()
operation returns a list of PNGetMessageActionsResult
objects, each containing the following operations:
Method | Type | Description |
---|---|---|
getType() | String | Message action's type. |
getValue() | String | Message action's value. |
getUuid() | String | Publisher of the message action. |
getActionTimetoken() | Long | Timestamp when the message action was created. |
getMessageTimetoken() | Long | Timestamp when the actual message was created the message action belongs to. |
Other Examples
Fetch Messages with paging
public static void main(String args[]){
getMessageActionsWithPaging("my_channel", System.currentTimeMillis() * 10_000L, new Callback() {
@Override
public void onMore(List<PNMessageAction> actions) {
System.out.println("Next set of actions: " + actions.size());
}
@Override
public void onDone() {
System.out.println("All actions fetched");
}
});
}
/**
show all 44 lines