Message Actions API for Android SDK
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 the support article.
Add an action to a published message. The response includes the added action.
Method(s)
Use this Android method:
this.pubnub.addMessageAction()
.channel(String)
.messageAction(PNMessageAction);
Parameter | Description |
---|---|
channel *Type: String | Channel name to add the message action to. |
messageAction *Type: PNMessageAction | Message action payload (type, value, message timetoken). |
async *Type: PNCallback | Callback of type PNAddMessageActionResult . |
Sample code
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());
show all 21 linesReturns
The addMessageAction()
operation returns a PNAddMessageActionResult
which contains the following operations:
Method | Description |
---|---|
getType() Type: String | Message action type. |
getValue() Type: String | Message action value. |
getUuid() Type: String | Publisher of the message action. |
getActionTimetoken() Type: Long | Timestamp when the message action was created. |
getMessageTimetoken() Type: Long | Timestamp when the message was created that the action belongs to. |
PNMessageAction
Method | Description |
---|---|
setType() Type: String | Message action type. |
setValue() Type: String | Message action value. |
setMessageTimetoken() 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 Android method:
this.pubnub.removeMessageAction()
.channel(String)
.messageTimetoken(Long)
.actionTimetoken(Long);
Parameter | Description |
---|---|
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. |
async *Type: PNCallback | Callback of type PNRemoveMessageActionResult . |
Sample code
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();
}
}
});
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.
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.
Method(s)
Use this Android method:
this.pubnub.getMessageActions()
.channel(String)
.start(Long)
.end(Long)
.limit(Integer);
Parameter | Description |
---|---|
channel *Type: String Default: n/a | Channel name to list message actions for. |
start Type: Long Default: n/a | Message action timetoken for the start of the range (exclusive). |
end Type: Long Default: n/a | Message action timetoken for the end of the range (inclusive). |
limit Type: Integer Default: 100 | Maximum number of actions to return. Default/Maximum is 100 . |
async *Type: PNCallback Default: n/a | Callback of type PNGetMessageActionsResult . |
Sample code
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 {
show all 19 linesReturns
The getMessageActions()
operation returns a list of PNGetMessageActionsResult
objects, each containing the following operations:
Method | Description |
---|---|
getType() Type: String | Message action type. |
getValue() Type: String | Message action value. |
getUuid() Type: String | Publisher of the message action. |
getActionTimetoken() Type: Long | Timestamp when the message action was created. |
getMessageTimetoken() Type: Long | Timestamp when the message was created that the 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 45 lines