Utility Methods API for Android SDK

Unsupported docs

PubNub no longer maintains Android SDK docs, but our Java SDK or Kotlin SDK are fully compatible with the Android platform and you can use them to build mobile apps, ensuring stable software development.

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()
2 .setApnsPayload(PushPayloadHelper.APNSPayload)
3 .setFcmPayload(PushPayloadHelper.FCMPayload)
4 .setCommonPayload(Map<String, Object>)
5 .build()
* required
ParameterDescription
setApnsPayload()
Type: APNSPayload
Set APNs and APNs2 Payload. Associated devices will receive only the data supplied here within the pn_apns key.
setFcmPayload()
Type: FCMPayload
Set FCM Payload. Associated devices will receive only the data supplied here within the pn_fcm key.
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.
build() *
Type: Map<String, Object>
Builds the payload from the values set using the parameters.

Sample code​

Create push payload​

1// Create an instance of PushPayloadHelper
2PushPayloadHelper pushPayloadHelper = new PushPayloadHelper();
3
4// Setup FCM parameters (FCMPayload)
5PushPayloadHelper.FCMPayload fcmPayload = new PushPayloadHelper.FCMPayload();
6// The FCMPayload includes a custom Notification object, which FCM itself uses
7// to display the message automatically to end-user devices on behalf of the
8// client app.
9// Notification messages have a predefined set of user-visible keys
10PushPayloadHelper.FCMPayload.Notification fcmNotification =
11 new PushPayloadHelper.FCMPayload.Notification()
12 .setTitle("Notification title")
13 .setBody("Notification body text")
14 .setImage("http://example.com/image.png");
15fcmPayload.setNotification(fcmNotification);
show all 82 lines

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​

1pubnub.destroy();

Returns​

None

Encrypt​

This function allows to encrypt the data.

Method(s)​

To encrypt the data you can use the following method(s) in Android SDK.

1pubnub.encrypt(data, customCipherKey)
* required
ParameterDescription
data *
Type: String
The data to encrypt.
customCipherKey
Type: String
If it's not provided, the cipher key from config will be used.

Sample code​

Encrypt part of message​

1JSONParser parser = new JSONParser();
2String stringToParse = "hello world";
3JSONObject json = (JSONObject) parser.parse(stringToParse);
4String encodedKey = "testCypher";
5
6String payload = pubnub.encrypt(json, encodedKey);

Encrypt file input stream​

Encrypts input stream with a cipher key.

Method(s)​

1pubnub.encryptInputStream(inputStream, cipherKey)
* required
ParameterDescription
inputStream *
Type: InputStream
Default:
n/a
Stream with content to encrypt.
cipherKey
Type: String
Default:
PNConfiguration.getCipherKey()
Cipher key used for encryption.

Sample code​

1pubnub.encryptInputStream(InputStream inputStream);
2pubnub.encryptInputStream(InputStream inputStream, String cipherKey);

Returns​

InputStream with encrypted data.

Decrypt​

This function allows to decrypt the data.

Method(s)​

To decrypt the data you can use the following method(s) in Android SDK.

1pubnub.decrypt(data, customCipherKey)
* required
ParameterDescription
data *
Type: String
The data to decrypt.
customCipherKey
Type: String
If it's not provided, the cipher key from config will be used.

Decrypt file input stream​

Decrypts input stream with a cipher key.

Method(s)​

1pubnub.decryptInputStream(inputStream, cipherKey)
* required
ParameterDescription
inputStream *
Type: InputStream
Default:
n/a
Stream with content encrypted data.
cipherKey
Type: String
Default:
PNConfiguration.getCipherKey()
Cipher key used for encryption.

Sample code​

1FileInputStream encryptedInputStream = new FileInputStream("./cat_picture_encrypted.jpg");
2InputStream decryptedInputStream = pubnub.decryptInputStream(encryptedInputStream, "myCipherKey");

Returns​

InputStream with decrypted data.

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 Android SDK:

1public final List<String> getSubscribedChannelGroups()

Sample code​

Get subscribed channel groups​

1List<String> groups = pubnub.getSubscribedChannelGroups();

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 Android SDK:

1public final List<String> getSubscribedChannels()

Sample code​

Get subscribed channels​

1List<String> channels = pubnub.getSubscribedChannels();

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

1disconnect()

This method doesn't take any arguments.

Sample code​

1pubnub.disconnect();

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

1reconnect()

This method doesn't take any arguments.

Sample code​

1pubnub.reconnect();

Was this page useful?

Last updated on