AndroidAndroid V4 Message Actions API Reference for Realtime Apps

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 PubNub Storage independently or when they fetch original messages.


Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.

Add an action on a published message. Returns the added action in the response.

To Add a Message Action you can use the following method(s) in the Android V4 SDK:

  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel name to publish message actions to.
    messageActionPNMessageActionYesThe message action object containing the message action's type, value and the publish timetoken of the original message.
    asyncPNCallbackYesPNCallback of type PNAddMessageActionResult.
pubnub.addMessageAction()
        .channel("my_channel")
        .messageAction(new PNMessageAction()
                .setType("reaction")
                .setValue("smiley_face")
                .setMessageTimetoken(15701761818730000L)
        )
        .async(new PNCallback<PNAddMessageActionResult>() {
            @Override
            public void onResponse(PNAddMessageActionResult result, PNStatus status) {
                if (!status.isError()) {
                    System.out.println(result.getType());
                    System.out.println(result.getValue());
                    System.out.println(result.getUuid());
                    System.out.println(result.getActionTimetoken());
                    System.out.println(result.getMessageTimetoken());
                } else {
                    status.getErrorData().getThrowable().printStackTrace();
                }
            }
        })

The addMessageAction() operation returns a PNAddMessageActionResult which contains the following operations:

MethodTypeDescription
getType()StringMessage action’s type.
getValue()StringMessage action’s value.
getUuid()StringPublisher of the message action.
getActionTimetoken()LongTimestamp when the message action was created.
getMessageTimetoken()LongTimestamp when the actual message was created the message action belongs to.
MethodTypeDescription
setType()StringMessage action’s type.
setValue()StringMessage action’s value.
setMessageTimetoken()LongTimestamp of the actual message was created the message action belongs to.

Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.

Remove a peviously added action on a published message. Returns an empty response.

To Remove a Message Action you can use the following method(s) in the Android V4 SDK:

  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel name to publish message actions to.
    messageTimetokenLongYesPublish timetoken of the original message
    actionTimetokenLongYesPublish timetoken of the message action to be removed.
    asyncPNCallbackYesPNCallback of type PNRemoveMessageActionResult.
pubnub.removeMessageAction()
        .channel("my_channel")
        .messageTimetoken(15701761818730000L)
        .actionTimetoken(15701775691010000L)
        .async(new PNCallback<PNRemoveMessageActionResult>() {
            @Override
            public void onResponse(PNRemoveMessageActionResult result, PNStatus status) {
                if (!status.isError()) {
                    // result has no actionable data
                    // it's enough to check if the status itself is not an error 
                } else {
                    status.getErrorData().getThrowable().printStackTrace();
                }
            }
        });

The removeMessageAction() operation returns a no actionable data.


Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.

Get a list of message actions in a channel. Returns a list of actions in the response.

To Get Message Actions you can use the following method(s) in the Android V4 SDK:

  1. ParameterTypeRequiredDefaultsDescription
    channelStringYesThe channel name.
    startLongOptionalMessage Action timetoken denoting the start of the range requested (return values will be less than start).
    endLongOptionalMessage Action timetoken denoting the end of the range requested (return values will be greater than or equal to end).
    limitIntegerOptional100Specifies the number of actions to return in response.
    Default/Maximum is 100.
    asyncPNCallbackYesPNCallback of type PNGetMessageActionsResult.
pubnub.getMessageActions()
        .channel("my_channel")
        .async(new PNCallback<PNGetMessageActionsResult>() {
            @Override
            public void onResponse(PNGetMessageActionsResult result, PNStatus status) {
                if (!status.isError()) {
                    List<PNMessageAction> actions = result.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());
                    }
                } else {
                    status.getErrorData().getThrowable().printStackTrace();
                }
            }
        });

The getMessageActions() operation returns a list of PNGetMessageActionsResult objects, each containing the following operations:

MethodTypeDescription
getType()StringMessage action’s type.
getValue()StringMessage action’s value.
getUuid()StringPublisher of the message action.
getActionTimetoken()LongTimestamp when the message action was created.
getMessageTimetoken()LongTimestamp when the actual message was created the message action belongs to.
  1. 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");
                }
            });
    }
    
    /**
     * Fetches 5 message actions at a time, recursively and in a paged manner.
     *
     * @param channel  The channel where the message is published, to fetch message actions from.
     * @param start    The timetoken which indicates from where to start fetching message actions.
     * @param callback The callback to dispatch fetched message actions to.
     */
    static void getMessageActionsWithPaging(String channel, Long start, Callback callback) {
        pubnub.getMessageActions()
                .channel(channel)
                .start(start)
                .limit(5)
                .async(new PNCallback<PNGetMessageActionsResult>() {
                    @Override
                    public void onResponse(PNGetMessageActionsResult result, PNStatus status) {
                        if (!status.isError() && !result.getActions().isEmpty()) {
                            callback.onMore(result.getActions());
                            getMessageActionsWithPaging(channel, 
                                result.getActions().get(0).getActionTimetoken(), callback);
                        } else {
                            callback.onDone();
                        }
                    }
                });
    }
    
    interface Callback {
        void onMore(List<PNMessageAction> actions);
    
        void onDone();
    }