Message Persistence API for PubNub Android SDK
Message Persistence provides real-time access to the history of all messages published to PubNub. Each published message is timestamped to the nearest 10 nanoseconds and is stored across multiple availability zones in several geographical locations. Stored messages can be encrypted with AES-256 message encryption, ensuring that they are not readable while stored on PubNub's network. For more information, refer to Message Persistence.
Messages can be stored for a configurable duration or forever, as controlled by the retention policy that is configured on your account. The following options are available: 1 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.
You can retrieve the following:
- Messages
- Message reactions
- File Sharing (using File Sharing API)
Fetch History
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
This function fetches historical messages from one or multiple channels. The includeMessageActions
flag also allows you to fetch message reactions along with the messages.
It's possible to control how messages are returned and in what order.
- if you specify only the
start
parameter (withoutend
), you will receive messages that are older than thestart
timetoken - if you specify only the
end
parameter (withoutstart
), you will receive messages from thatend
timetoken and newer - if you specify values for both
start
andend
parameters, you will retrieve messages between those timetokens (inclusive of theend
value)
You will receive a maximum of 100 messages for a single channel or 25 messages for multiple channels (up to 500). If more messages meet the timetoken criteria, make iterative calls while adjusting the start
timetoken to fetch the entire list of messages from Message Persistence.
Method(s)
To run Fetch History
, you can use the following method(s) in the Android SDK:
this.pubnub.fetchMessages()
.channels(List<String>)
.start(Long)
.end(Long)
.maximumPerChannel(Integer)
.includeMeta(Boolean)
.includeMessageActions(Boolean)
.includeMessageType(Boolean)
.includeUUID(Boolean)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channels | List<String> | Yes | Specifies channels to return history messages from. Maximum of 500 channels are allowed. | |
start | Long | Optional | Timetoken delimiting the start of time slice (exclusive) to pull messages from. | |
end | Long | Optional | Timetoken delimiting the end of time slice (inclusive) to pull messages from. | |
maximumPerChannel | Integer | Optional | 100 or 25 | Specifies the number of historical messages to return per channel. Maximum value is 100 for a single channel or 25 for multiple channels. |
includeMeta | Boolean | Optional | false | Whether to include message metadata within response or not. |
includeMessageActions | Boolean | Optional | false | The flag denoting to retrieve history messages with message reactions. If true , the method is limited to one channel only. |
includeMessageType | Boolean | Optional | true | The flag denoting to retrieve history messages with message type. If includeMessageActions is true , then 25 is the default (and maximum) value. |
includeUUID | Boolean | Optional | true | The flag denoting to retrieve history messages with UUID of a publisher. |
async | PNCallback | Yes | PNCallback of type PNFetchMessagesResult . |
Truncated response
If you fetch messages with messages actions, the number of messages 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 history adjusting the parameters to fetch more messages.
Basic Usage
Retrieve the last message on a channel:
pubnub.fetchMessages()
.channels(Arrays.asList("my_channel"))
.maximumPerChannel(25)
.includeMessageActions(true)
.includeMeta(true)
.includeMessageType(true)
.includeUUID(true)
.async(new PNCallback<PNFetchMessagesResult>() {
@Override
public void onResponse(PNFetchMessagesResult result, PNStatus status) {
if (!status.isError()) {
Map<String, List<PNFetchMessageItem>> channels = result.getChannels();
for (PNFetchMessageItem messageItem : channels.get("my_channel")) {
System.out.println(messageItem.getMessage());
System.out.println(messageItem.getMeta());
show all 36 linesReturns
The fetchMessages()
operation returns a list of PNFetchMessagesResult
objects, each containing the following operations:
Method | Type | Description |
---|---|---|
getMessage() | JsonElement | Message content. |
getMeta() | JsonElement | Message metadata if any, and if requested via includeMeta(true) . |
getTimetoken() | Long | Publish timetoken. |
getActionTimetoken() | Long | Timestamp when the message reaction was created. |
getActions() | HashMap | Actions data of the message, if any, and if requested via includeMessageActions(true) . |
getMessageType() | Integer | Message type 0 - message, 1 - signal, 2 - object, 3 - message reaction, 4 - files |
getUuid() | String | UUID of the publisher |
Other Examples
Paging History Responses
package com.we.pubnubtest.utils;
import com.pubnub.api.models.consumer.history.PNFetchMessagesResult;
public abstract class CallbackSkeleton {
public CallbackSkeleton() {
}
public abstract void handleResponse(PNFetchMessagesResult result);
public abstract void finish();
}
package com.we.pubnubtest.utils;
import com.pubnub.api.PubNub;
import com.pubnub.api.callbacks.PNCallback;
import com.pubnub.api.models.consumer.PNStatus;
import com.pubnub.api.models.consumer.history.PNFetchMessagesResult;
import com.pubnub.api.models.consumer.pubsub.PNMessageResult;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class PubnubRecursiveHistoryFetcher {
private PubNub pubNub;
private String channels;
show all 66 linesFetch Messages From Multiple Channels
pubNub.fetchMessages()
.channels(Arrays.asList("ch1", "ch2", "ch3"))
.async(new PNCallback<PNFetchMessagesResult>() {
@Override
public void onResponse(@Nullable final PNFetchMessagesResult result, @NotNull final PNStatus status) {
if (!status.isError()) {
final Map<String, List<PNFetchMessageItem>> channelToMessageItemsMap = result.getChannels();
final Set<String> channels = channelToMessageItemsMap.keySet();
for (final String channel : channels) {
List<PNFetchMessageItem> pnFetchMessageItems = channelToMessageItemsMap.get(channel);
for (final PNFetchMessageItem fetchMessageItem: pnFetchMessageItems) {
System.out.println(fetchMessageItem.getMessage());
System.out.println(fetchMessageItem.getMeta());
System.out.println(fetchMessageItem.getTimetoken());
}
show all 22 linesDelete Messages from History
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
Removes the messages from the history of a specific channel.
Required setting
There is a setting to accept delete from history requests for a key, which you must enable by checking the Enable Delete-From-History
checkbox in the key settings for your key in the Admin Portal.
Requires Initialization with secret key.
Method(s)
To Delete Messages from History
you can use the following method(s) in the Android SDK.
this.pubnub.deleteMessages()
.channels(Array)
.start(Long)
.end(Long)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channels | Array | Yes | Specifies channels to delete messages from. | |
start | Long | Optional | Timetoken delimiting the start of time slice (inclusive) to delete messages from. | |
end | Long | Optional | Timetoken delimiting the end of time slice (exclusive) to delete messages from. | |
async | PNCallback | Yes | PNCallback of type PNDeleteMessagesResult . |
Basic Usage
pubnub.deleteMessages()
.channels(Arrays.asList("channel_1", "channel_2"))
.start(1460693607379L)
.end(1460893617271L)
.async(new PNCallback<PNDeleteMessagesResult>() {
@Override
public void onResponse(PNDeleteMessagesResult result, PNStatus status) {
// The deleteMessages() method does not return actionable data, be sure to check the status
// object on the outcome of the operation by checking the status.isError().
}
});