Utility Methods API for Java SDK

Breaking changes in v9.0.0

PubNub Java SDK version 9.0.0 unifies the codebases for Java and Kotlin SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0) of the Java SDK.

For more details about what has changed, refer to Java/Kotlin SDK migration guide.

The methods on this page are utility methods that don't fit into other categories.

Create push payload​

This method creates the push payload for use in the appropriate endpoint calls.

Method(s)​

1PushPayloadHelper helper = new PushPayloadHelper();
2 helper.setApnsPayload(PushPayloadHelper.APNSPayload());
3 helper.setFcmPayloadV2(PushPayloadHelper.FCMPayloadV2());
4 helper.setCommonPayload(HashMap<String, Object>());
5
6Map<String, Object> payload = helper.build();
* required
ParameterDescription
helper.setApnsPayload()
Type: APNSPayload
Set APNs and APNs2 Payload. Associated devices will receive only the data supplied here within the pn_apns key.
helper.setFcmPayloadV2()
Type: FCMPayloadV2
Set FCM Payload V2. Associated devices will receive only the data supplied here within the pn_fcm key.
helper.setCommonPayload()
Type: Map<String, Object>
Set common Payload. Native PubNub subscribers will receive the data provided here, together with the pn_apns, and pn_fcm objects.
helper.build() *
Type: Map<String, Object>
Builds the payload from the values set using the parameters.

Sample code​

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Create push payload​

1

Response​

The PushPayloadHelper#build() operation returns a Map<String, Object> which can be passed directly as the message() parameter to the pubnub.publish() method.

Destroy​

Destroy frees up the threads and allows for clean exit.

Method(s)​

1destroy()

Sample code​

1

Returns​

None

Get subscribed channel groups​

Returns all the subscribed channel groups in a List of type String.

Method(s)​

To Get Subscribe Channel Groups you can use the following method(s) in the Java SDK:

1public final List<String> getSubscribedChannelGroups()

Sample code​

Get subscribed channel groups​

1

Response​

List<String>

1["channelGroup1", "channelGroup2"]

Get subscribed channels​

Returns all the subscribed channels in a List of type String.

Method(s)​

To Get Subscribed Channels you can use the following method(s) in the Java SDK:

1public final List<String> getSubscribedChannels()

Sample code​

Get subscribed channels​

1

Response​

List<String>

1["channel1", "channel2"]

Disconnect​

Call the disconnect method to force the SDK to stop all requests to PubNub server when there are active subscribe channels.

Method(s)​

To disconnect the data transmission you can use the following method(s) in Java SDK.

1disconnect()

This method doesn't take any arguments.

Sample code​

1

Reconnect​

Call the reconnect method to force the SDK to try and reach out PubNub.

Method(s)​

To reconnect the data you can use the following method(s) in Java SDK.

1reconnect(long timetoken)
* required
ParameterDescription
timetoken
Type: long
Timetoken to reconnect from.

Sample code​

1

Timetoken to date​

The timetokenToInstant() method of the TimetokenUtil class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to an Instant object representing the corresponding date and time.

Use this method when you want to display the timetoken of each message or event in the message history in a human-readable format.

Method signature​

1Instant TimetokenUtil.timetokenToInstant(long timetoken)

Input​

* required
ParameterDescription
timetoken *
Type: long
Default:
n/a
Represents the PubNub timetoken to convert into a human-readable date format.

Output​

TypeDescription
InstantThe human-readable date representation of the timetoken.

Sample code​

Convert a timetoken value of 17276954606232118 to a human-readable date and time format.

1

The output of the method is as follows:

PubNub timetoken: 17276954606232118
Current date: 2024-09-30
Current time: 11:24:20.623211800

Date to timetoken​

The instantToTimetoken() method of the TimetokenUtil class converts the Instant object representing the corresponding date and time into a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).

Use this method to represent a specific date and time as a PubNub timetoken value, for example, to retrieve messages from a PubNub channel at a particular date and time.

Method signature​

1long TimetokenUtil.instantToTimetoken(Instant instant)

Input​

* required
ParameterDescription
instant *
Type: Instant
Default:
n/a
Represents the date and time to convert into a PubNub timetoken.

Output​

TypeDescription
longConverted timetoken value.

Sample code​

Convert a human-readable date and time, September 30, 2024 12:12:24 GMT, to a timetoken.

1

Current date: 2024-09-30
Current time: 12:12:44.123456789
PubNub timetoken: 17276983641234567

Unix timestamp to timetoken​

The unixToTimetoken() method of the TimetokenUtil class converts a Unix timestamp (a number of seconds since January 1, 1970) to a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).

Use this method when you need a timetoken to retrieve historical messages with a specific timestamp range from Message Persistence.

Method signature​

This method takes the following parameters:

1long TimetokenUtil.unixToTimetoken(long unixTime)

Input​

* required
ParameterDescription
unixTime *
Type: long
Default:
n/a
Represents the Unix timestamp to convert into a PubNub timetoken.

Output​

TypeDescription
longConverted timetoken value.

Sample code​

Convert a Unix timestamp value of 1727866935316 representing 2024-10-02 11:02:15.316 to PubNub timetoken:

1

The output of the method is as follows:

PubNub timetoken: 17278669353160000
Current date: 2024-10-02
Current time: 11:02:15.316

Timetoken to Unix timestamp​

The timetokenToUnix() method of the TimetokenUtil class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to a Unix timestamp (a number of seconds since January 1, 1970).

Use this method to convert PubNub timetoken for use in another context or system that requires a Unix timestamp.

Method signature​

1long TimetokenUtil.timetokenToUnix(long timetoken)

Input​

* required
ParameterDescription
timetoken *
Type: long
Default:
n/a
Represents the PubNub timetoken to convert into a Unix timestamp.

Output​

TypeDescription
longConverted Unix timestamp value.

Sample code​

Convert a PubNub timetoken 17276954606232118 representing 2024-09-30 11:24:20.623211800 to Unix time:

1

The output of the method is as follows:

Current date: 2024-09-30
Current time: 11:24:20.623
PubNub timetoken: 17276954606232118

Was this page useful?

Last updated on