Message Persistence API for PubNub Android SDK

The message persistence service 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, 3, 5, 7, 15, or 30 days, and Forever.

You can retrieve the following:

  • Messages
  • Message actions
  • Files (using Files API)

Fetch History

Requires Message Persistence add-on

This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Description

This function fetches historical messages from one or multiple channels. The includeMessageActions flag also allows you to fetch message actions along with the messages.

It's possible to control how messages are returned and in what order.

  • if you specify only the start parameter (without end), you will receive messages that are older than the start timetoken
  • if you specify only the end parameter (without start), you will receive messages from that end timetoken and newer
  • if you specify values for both start and end parameters, you will retrieve messages between those timetokens (inclusive of the end 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 storage.

Method(s)

To run Fetch History, you can use the following method(s) in the Android V4 SDK:

this.pubnub.fetchMessages().channels(List<String>).maximumPerChannel(Integer).start(Long).end(Long).includeMessageActions(Boolean).includeMeta(Boolean).includeMessageType(Boolean).includeUUID(Boolean)
ParameterTypeRequiredDefaultDescription
channelsList<String>YesSpecifies channels to return history messages from. Maximum of 500 channels are allowed.
startLongOptionalTimetoken delimiting the start of time slice (exclusive) to pull messages from.
endLongOptionalTimetoken delimiting the end of time slice (inclusive) to pull messages from.
maximumPerChannelIntegerOptional100 or 25Specifies the number of historical messages to return per channel. Maximum value is 100 for a single channel, or 25 for multiple channels. If includeMessageActions is true, then 25 is the default (and maximum) value.
includeMetaBooleanOptionalfalseWhether to include message metadata within response or not.
includeMessageActionsBooleanOptionalfalseThe flag denoting to retrieve history messages with message actions. If true, the method is limited to one channel only.
includeMessageTypeBooleanOptionaltrueThe flag denoting to retrieve history messages with message type.
includeUUIDBooleanOptionaltrueThe flag denoting to retrieve history messages with UUID of a publisher.
asyncPNCallbackYesPNCallback 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 lines

Returns

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

MethodTypeDescription
getMessage()JsonElementMessage content.
getMeta()JsonElementMessage metadata if any, and if requested via includeMeta(true).
getTimetoken()LongPublish timetoken.
getActionTimetoken()LongTimestamp when the message action was created.
getActions()HashMapActions data of the message, if any, and if requested via includeMessageActions(true).
getMessageType()IntegerMessage type 0 - message, 1 - signal, 2 - object, 3 - message action, 4 - files
getUuid()StringUUID 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 lines

Fetch 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 lines

Delete Messages from History

Requires Message Persistence add-on

This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Description

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 V4 SDK.

this.pubnub.deleteMessages().channels(Array).start(Long).end(Long)
ParameterTypeRequiredDefaultDescription
channelsArrayYesSpecifies channels to delete messages from.
startLongOptionalTimetoken delimiting the start of time slice (inclusive) to delete messages from.
endLongOptionalTimetoken delimiting the end of time slice (exclusive) to delete messages from.
asyncPNCallbackYesPNCallback 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().
}
});

Other Examples

Delete specific message from history

To delete a specific message, pass the publish timetoken (received from a successful publish) in the End parameter and timetoken +/- 1 in the Start parameter. For example, if 15526611838554310 is the publish timetoken, pass 15526611838554309 in Start and 15526611838554310 in End parameters respectively as shown in the following code snippet.

pubnub.deleteMessages()
.channels(Arrays.asList("channel_1"))
.start(15526611838554310L)
.end(15526611838554309L)
.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().
}
});

Message Counts

Requires Message Persistence add-on

This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Description

Returns the number of messages published on one or more channels since a given time. The count returned is the number of messages in history with a timetoken value greater than or equal to than the passed value in the channelsTimetokenparameter.

Unlimited message retention

For keys with unlimited message retention enabled, this method considers only messages published in the last 30 days.

Method(s)

You can use the following method(s) in the Android V4 SDK:

this.pubnub.messageCounts().channels(Array).channelsTimetoken(Array)
ParameterTypeRequiredDefaultDescription
channelsArrayYesThe channels to fetch the message count
channelsTimetokenArrayYesList of timetokens, in order of the channels list. Specify a single timetoken to apply it to all channels. Otherwise, the list of timetokens must be the same length as the list of channels, or the function returns a PNStatuswith an error flag.
asyncPNCallbackYesPNCallback of type PNMessageCountResult.

Basic Usage

Long lastHourTimetoken = (Calendar.getInstance().getTimeInMillis() - TimeUnit.HOURS.toMillis(1)) * 10000L;

pubnub.messageCounts()
.channels(Arrays.asList("news"))
.channelsTimetoken(Arrays.asList(lastHourTimetoken))
.async(new PNCallback<PNMessageCountResult>() {
@Override
public void onResponse(PNMessageCountResult result, PNStatus status) {
if (!status.isError()) {
for (Map.Entry<String, Long> messageCountEntry : result.getChannels().entrySet()) {
messageCountEntry.getKey(); // the channel name
messageCountEntry.getValue(); // number of messages for that channel
}
} else {
// Handle error accordingly.
show all 19 lines

Returns

The operation returns a PNMessageCountResult which contains the following operations

MethodTypeDescription
getChannels()Map<String, Long>A map with values of Long for each channel. Channels without messages have a count of 0. Channels with 10,000 messages or more have a count of 10000.

Other Examples

Retrieve count of messages using different timetokens for each channel

Long lastHourTimetoken = (Calendar.getInstance().getTimeInMillis() - TimeUnit.HOURS.toMillis(1)) * 10000L;
Long lastDayTimetoken = (Calendar.getInstance().getTimeInMillis() - TimeUnit.DAYS.toMillis(1)) * 10000L;

pubnub.messageCounts()
.channels(Arrays.asList("news", "info"))
.channelsTimetoken(Arrays.asList(lastHourTimetoken, lastDayTimetoken))
.async(new PNCallback<PNMessageCountResult>() {
@Override
public void onResponse(PNMessageCountResult result, PNStatus status) {
if (!status.isError()) {
for (Map.Entry<String, Long> messageCountEntry : result.getChannels().entrySet()) {
messageCountEntry.getKey(); // the channel name
messageCountEntry.getValue(); // number of messages for that channel
}
} else {
show all 20 lines

History (deprecated)

Requires Message Persistence add-on

This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Alternative method

This method is deprecated. Use fetch history instead.

Description

This function fetches historical messages of a channel.

It is possible to control how messages are returned and in what order, for example you can:

  • Search for messages starting on the newest end of the timeline (default behavior - reverse = false)
  • Search for messages from the oldest end of the timeline by setting reverse to true.
  • Page through results by providing a start OR end timetoken.
  • Retrieve a slice of the time line by providing both a start AND end timetoken.
  • Limit the number of messages to a specific quantity using the count parameter.
Start & End parameter usage clarity

If only the start parameter is specified (without end), you will receive messages that are older than and up to that start timetoken value. If only the end parameter is specified (without start) you will receive messages that match that end timetoken value and newer. Specifying values for both start and end parameters will return messages between those timetoken values (inclusive on the end value). Keep in mind that you will still receive a maximum of 100 messages even if there are more messages that meet the timetoken values. Iterative calls to history adjusting the start timetoken is necessary to page through the full set of results if more than 100 messages meet the timetoken values.

Method(s)

To run History you can use the following method(s) in the Android V4 SDK:

this.pubnub.history().channel(String).reverse(Boolean).includeTimetoken(Boolean).includeMeta(Boolean).start(Long).end(Long).count(Integer);
ParameterTypeRequiredDefaultDescription
channelStringYesSpecifies channelto return history messages from.
reverseBooleanOptionalfalseSetting to true traverses the time line in reverse, starting with the oldest message first.
includeTimetokenBooleanOptionalfalseWhether event dates timetokens should be included in response or not.
includeMetaBooleanOptionalfalseWhether to include message metadata within response or not.
startLongOptionalTimetoken delimiting the start of time slice (exclusive) to pull messages from.
endLongOptionalTimetoken delimiting the end of time slice (inclusive) to pull messages from.
countIntOptional100Specifies the number of historical messages to return.
asyncPNCallbackYesPNCallback of type PNHistoryResult.
Using the reverse parameter

Messages are always returned sorted in ascending time direction from history regardless of reverse. The reverse direction matters when you have more than 100 (or count, if it's set) messages in the time interval, in which case reverse determines the end of the time interval from which it should start retrieving the messages.

Basic Usage

Retrieve the last 100 messages on a channel:

pubnub.history()
.channel("history_channel") // where to fetch history from
.count(100) // how many items to fetch
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {

}
});

Returns

The history() operation returns a PNHistoryResult which contains the following operations:

MethodTypeDescription
getMessages()List<PNHistoryItemResult>List of messages of type PNHistoryItemResult. See PNHistoryItemResult for more details.
getStartTimetoken()LongStart timetoken.
getEndTimetoken()LongEnd timetoken.

PNHistoryItemResult

MethodTypeDescription
getTimetoken()LongTimetoken of the message.
getEntry()JsonElementMessage.

Other Examples

Use history() to retrieve the three oldest messages by retrieving from the time line in reverse

pubnub.history()
.channel("my_channel") // where to fetch history from
.count(3) // how many items to fetch
.reverse(true) // should go in reverse?
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {

}
});

Response

if (!status.isError()) {
for (PNHistoryItemResult pnHistoryItemResult: result.getMessages()) {
pnHistoryItemResult.getEntry(); // custom JSON structure for message
}
}

Use history() to retrieve messages newer than a given timetoken by paging from oldest message to newest message starting at a single point in time (exclusive)

pubnub.history()
.channel("my_channel") // where to fetch history from
.start(13847168620721752L) // first timestamp
.reverse(true) // should go in reverse?
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {

}
});
Response
if (!status.isError()) {
for (PNHistoryItemResult pnHistoryItemResult: result.getMessages()) {
pnHistoryItemResult.getEntry(); // custom JSON structure for message
}
}

Use history() to retrieve messages until a given timetoken by paging from newest message to oldest message until a specific end point in time (inclusive)

pubnub.history()
.channel("my_channel") // where to fetch history from
.count(100) // how many items to fetch
.start(-1) // first timestamp
.end(13847168819178600L) // last timestamp
.reverse(true) // should go in reverse?
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {

}
});
Response
[
["Pub3","Pub4","Pub5"],
13406746780720711,
13406746845892666
]

History Paging Example

Usage

You can call the method by passing 0 or a valid timetoken as the argument.

package com.pubnub.api;

import com.pubnub.api.PNConfiguration;
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.PNHistoryItemResult;
import com.pubnub.api.models.consumer.history.PNHistoryResult;

public class PubNubRecursiveHistoryFetcher {

private PubNub pubnub;

private static abstract class CallbackSkeleton {
public abstract void handleResponse(PNHistoryResult result);
show all 63 lines

Include timetoken in history response

pubnub.history()
.channel("history_channel") // where to fetch history from
.count(100) // how many items to fetch
.includeTimetoken(true) // include timetoken with each entry
.async(new PNCallback<PNHistoryResult>() {
@Override
public void onResponse(PNHistoryResult result, PNStatus status) {

}
});
Last updated on