Message Actions API for Android SDK
Unsupported docs
PubNub no longer maintains Android SDK docs, but our Java SDK or Kotlin SDK are fully compatible with the Android platform and you can use them to build mobile apps, ensuring stable software development.
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.
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 Enable the features your app calls.
Add an action to a published message. The response includes the added action.
Method(s)
Use this Android method:
1this.pubnub.addMessageAction()
2 .channel(String)
3 .messageAction(PNMessageAction);
Sample code
1pubnub.addMessageAction()
2 .channel("my_channel")
3 .messageAction(new PNMessageAction()
4 .setType("reaction")
5 .setValue("smiley_face")
6 .setMessageTimetoken(15701761818730000L)
7 )
8 .async(new PNCallback<PNAddMessageActionResult>() {
9 @Override
10 public void onResponse(PNAddMessageActionResult result, PNStatus status) {
11 if (!status.isError()) {
12 System.out.println(result.getType());
13 System.out.println(result.getValue());
14 System.out.println(result.getUuid());
15 System.out.println(result.getActionTimetoken());
show all 21 linesReturns
The addMessageAction() operation returns a PNAddMessageActionResult which contains the following operations:
PNMessageAction
Remove message action
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal as described in Enable the features your app calls.
Remove a previously added action from a published message. The response is empty.
Method(s)
Use this Android method:
1this.pubnub.removeMessageAction()
2 .channel(String)
3 .messageTimetoken(Long)
4 .actionTimetoken(Long);
Sample code
1pubnub.removeMessageAction()
2 .channel("my_channel")
3 .messageTimetoken(15701761818730000L)
4 .actionTimetoken(15701775691010000L)
5 .async(new PNCallback<PNRemoveMessageActionResult>() {
6 @Override
7 public void onResponse(PNRemoveMessageActionResult result, PNStatus status) {
8 if (!status.isError()) {
9 // result has no actionable data
10 // it's enough to check if the status itself is not an error
11 } else {
12 status.getErrorData().getThrowable().printStackTrace();
13 }
14 }
15 });
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 Enable the features your app calls.
Get a list of message actions in a channel. The response sorts actions by the action timetoken in ascending order.
Truncated response
The number of message actions in the response may be truncated when internal limits are hit. If the response is truncated, a more property is returned with additional parameters. Send iterative calls to Message Persistence, adjusting the parameters to fetch more message actions.
How to use the start and end parameters
PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:
startis the newer boundary (higher timetoken value) - search begins here, exclusiveendis the older boundary (lower timetoken value) - search stops here, inclusive
When you provide both parameters, start must be a higher timetoken than end.
Results are always returned in oldest-first order.
Method(s)
Use this Android method:
1this.pubnub.getMessageActions()
2 .channel(String)
3 .start(Long)
4 .end(Long)
5 .limit(Integer);
channelrequired#- Channel name to list message actions for.
start#- Newer boundary of the query (exclusive). Must be a higher timetoken value than
endwhen both are provided. end#- Older boundary of the query (inclusive). Must be a lower timetoken value than
startwhen both are provided. limit#- Maximum number of actions to return. Default/Maximum is
100. asyncrequired#- Callback of type
PNGetMessageActionsResult.
Sample code
1pubnub.getMessageActions()
2 .channel("my_channel")
3 .async(new PNCallback<PNGetMessageActionsResult>() {
4 @Override
5 public void onResponse(PNGetMessageActionsResult result, PNStatus status) {
6 if (!status.isError()) {
7 List<PNMessageAction> actions = result.getActions();
8 for (PNMessageAction action : actions) {
9 System.out.println(action.getType());
10 System.out.println(action.getValue());
11 System.out.println(action.getUuid());
12 System.out.println(action.getActionTimetoken());
13 System.out.println(action.getMessageTimetoken());
14 }
15 } else {
show all 19 linesReturns
The getMessageActions() operation returns a list of PNGetMessageActionsResult objects, each containing the following operations:
Other examples
Fetch messages with paging
1public static void main(String args[]){
2 getMessageActionsWithPaging("my_channel", System.currentTimeMillis() * 10_000L, new Callback() {
3 @Override
4 public void onMore(List<PNMessageAction> actions) {
5 System.out.println("Next set of actions: " + actions.size());
6 }
7
8 @Override
9 public void onDone() {
10 System.out.println("All actions fetched");
11 }
12 });
13}
14
15/**
show all 45 lines