AndroidAndroid V4 Misc API Reference for Realtime Apps

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

  1. ParameterTypeRequiredDescription
    setApnsPayload()APNSPayloadOptionalSet APNS and APNS2 Payload. Associated devices will receive only the data supplied here within the pn_apns key.
    setMpnsPayload()MPNSPayloadOptionalSet MPNS Payload. Associated devices will receive only the data supplied here within the pn_mpns key.
    setFcmPayload()FCMPayloadOptionalSet FCM Payload. Associated devices will receive only the data supplied here within the pn_gcm key.
    setCommonPayload()Map<String, Object>OptionalSet common Payload. Native PubNub subscribers will receive the data provided here, together with the pn_apns, pn_gcm, and pn_mpns objects.
    build()Map<String, Object>YesBuilds the payload from the values set using the parameters.

Create Push Payload

// Create an instance of PushPayloadHelper
PushPayloadHelper pushPayloadHelper = new PushPayloadHelper();

// Setup FCM parameters (FCMPayload)
PushPayloadHelper.FCMPayload fcmPayload = new PushPayloadHelper.FCMPayload();
// The FCMPayload includes a custom Notification object, which FCM itself uses
// to display the message automatically to end-user devices on behalf of the
// client app.
// Notification messages have a predefined set of user-visible keys
PushPayloadHelper.FCMPayload.Notification fcmNotification =
        new PushPayloadHelper.FCMPayload.Notification()
                .setTitle("Notification title")
                .setBody("Notification body text")
                .setImage("http://example.com/image.png");
fcmPayload.setNotification(fcmNotification);
// Data messages contain only your user-defined custom key-value pairs
Map<String, Object> fcmData = new HashMap<>();
fcmData.put("city", "sf");
fcmData.put("count", 71);
fcmData.put("is_private", true);
fcmPayload.setData(fcmData);
// Specify top-level objects
Map<String, Object> fcmCustom = new HashMap<>();
fcmCustom.put("token", "bk3RNwTe3H0:CI2k_HvvDMExdFQ3P1...");
fcmCustom.put("topic", "/topic/news/");
fcmPayload.setCustom(fcmCustom);
// Set FCM payload
pushPayloadHelper.setFcmPayload(fcmPayload);

// Setup MPNS parameters (MPNSPayload) with a predefined set of properties
PushPayloadHelper.MPNSPayload mpnsPayload = new PushPayloadHelper.MPNSPayload();
mpnsPayload.setTitle("Title");
mpnsPayload.setType("Type");
mpnsPayload.setBackTitle("Back Title");
mpnsPayload.setBackContent("Back Content");
mpnsPayload.setCount(1);
// Specify top-level objects
Map<String, Object> mpnsCustom = new HashMap<>();
mpnsCustom.put("mpns_top_key_1", "value_1");
mpnsCustom.put("mpns_top_key_2", "value_2");
mpnsPayload.setCustom(mpnsCustom);
// Set MPNS payload
pushPayloadHelper.setMpnsPayload(mpnsPayload);

// Setup APNS parameters
PushPayloadHelper.APNSPayload apnsPayload = new PushPayloadHelper.APNSPayload();
// Define APS
PushPayloadHelper.APNSPayload.APS aps = new PushPayloadHelper.APNSPayload.APS()
        .setAlert("Alert")
        .setBadge(1)
        .setSound("Ding");
// Set APS
apnsPayload.setAps(aps);
// Set APNS2 Configurations as a list
apnsPayload.setApns2Configurations(Arrays.asList(
        new PushPayloadHelper.APNSPayload.APNS2Configuration()
                .setCollapseId("invitations")
                .setExpiration("2019-12-13T22:06:09Z")
                .setVersion("v1")
                .setTargets(Arrays.asList(
                        new PushPayloadHelper.APNSPayload.APNS2Configuration.Target()
                                .setEnvironment(PNPushEnvironment.DEVELOPMENT)
                                .setTopic("com.meetings.chat.app")
                                .setExcludeDevices(Arrays.asList("device1", "device2"))
                )),
        new PushPayloadHelper.APNSPayload.APNS2Configuration()
                .setCollapseId("invitations")
                .setExpiration("2019-12-15T22:06:09Z")
                .setVersion("v2")
                .setTargets(Arrays.asList(
                        new PushPayloadHelper.APNSPayload.APNS2Configuration.Target()
                                .setEnvironment(PNPushEnvironment.DEVELOPMENT)
                                .setTopic("com.meetings.chat.app")
                                .setExcludeDevices(Arrays.asList("device3", "device4"))
                ))
));
Map<String, Object> apnsCustom = new HashMap<>();
apnsCustom.put("apns_key_1", "value_1");
apnsCustom.put("apns_key_2", "value_2");
apnsPayload.setCustom(apnsCustom);
// Set APNS payload
pushPayloadHelper.setApnsPayload(apnsPayload);

// Common payload for native PubNub subscribers
Map<String, Object> commonPayload = new HashMap<>();
apnsCustom.put("message", "Hello");
apnsCustom.put("such", "object");
apnsCustom.put("type", 7);
pushPayloadHelper.setCommonPayload(commonPayload);

// Build the payload
// Returns a Map which can be used directly as the message for the pubnub.publish() method
Map<String, Object> payload = pushPayloadHelper.build();
pubnub.publish()
        .channel("foo")
        .message(payload)
        .async(...);

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

This function allow to decrypt the data.
To decrypt the data you can use the following method(s) in Android V4 SDK.
  1. ParameterTypeRequiredDescription
    dataStringYesThe data to decrypt.
    customCipherKeyStringOptionalIf it's not provided, the cipher key from config will be used.
Destroy frees up the threads and allows for clean exit.
  1. destroy()
pubnub.destroy();
None
This function allow to encrypt the data.
To encrypt the data you can use the following method(s) in Android V4 SDK.
  1. ParameterTypeRequiredDescription
    dataStringYesThe data to encrypt.
    customCipherKeyStringOptionalIf it's not provided, the cipher key from config will be used.
JSONParser parser = new JSONParser(); 
String stringToParse = "hello world";
JSONObject json = (JSONObject) parser.parse(stringToParse);
String encodedKey = "testCypher";

String payload = pubnub.encrypt(json, encodedKey);
Returns all the subscribed channel groups in a List of type String..
To Get Subscribe Channel Groups you can use the following method(s) in the Android V4 SDK
  1. public final List<String> getSubscribedChannelGroups()
List<String> groups = pubnub.getSubscribedChannelGroups();
List<String>

["channelGroup1", "channelGroup2"]
Returns all the subscribed channels in a List of type String..
To Get Subscribed Channels you can use the following method(s) in the Android V4 SDK:
  1. public final List<String> getSubscribedChannels()
List<String> channels = pubnub.getSubscribedChannels();
List<String>

["channel1", "channel2"]
Call the reconnect method to force the SDK to try and reach out PubNub.
To reconnect the data you can use the following method(s) in Android V4 SDK.
  1. This method doesn't take any arguments.

pubnub.reconnect();
This function will return a 17 digit precision Unix epoch.
 
Timetoken
The timetoken is constructed using the following algorithm:
timetoken = (Unix epoch time in seconds) * 10000000
Example of creating a timetoken for a specific time & date
08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000
To fetch Time you can use the following method(s) in Android V4 SDK
  1. ParameterTypeRequiredDescription
    asyncPNCallbackYesPNCallback of type PNTimeResult
pubnub.time().async(new PNCallback<PNTimeResult>() {
	@Override
	public void onResponse(PNTimeResult result, PNStatus status) {
		// handle time result.
	}
});
The time() operation returns a PNTimeResult which contains the following operations:
MethodTypeDescription
getTimetoken()LongReturns a long representation of current time token.