---
source_url: https://www.pubnub.com/docs/sdks/android/api-reference/misc
title: Utility Methods API for Android SDK
updated_at: 2026-05-27T17:01:21.810Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Utility Methods API for Android SDK

:::warning Unsupported docs
PubNub no longer maintains Android SDK docs, but our [Java SDK](https://www.pubnub.com/docs/sdks/java) or [Kotlin SDK](https://www.pubnub.com/docs/sdks/kotlin) 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)

```java
PushPayloadHelper()
        .setApnsPayload(PushPayloadHelper.APNSPayload)
        .setFcmPayload(PushPayloadHelper.FCMPayload)
        .setCommonPayload(Map<String, Object>)
        .build()
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| setApnsPayload() | APNSPayload | Optional |  | Set APNs and APNs2 Payload. Associated devices will receive only the data supplied here within the `pn_apns` key. |
| setFcmPayload() | FCMPayload | Optional |  | Set FCM Payload. Associated devices will receive only the data supplied here within the `pn_fcm` key. |
| setCommonPayload() | Map<String, | Optional |  | Set common Payload. Native PubNub subscribers will receive the data provided here, together with the `pn_apns`, and `pn_fcm` objects. |
| build() | Map<String, | Yes |  | Builds the payload from the values set using the parameters. |

### Sample code

#### Create push payload

```java
// 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 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<>();
commonPayload.put("message", "Hello");
commonPayload.put("such", "object");
commonPayload.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(...);
```

### 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)

```java
destroy()
```

### Sample code

```java
pubnub.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.

```java
pubnub.encrypt(data, customCipherKey)
```

| Parameter | Description |
| --- | --- |
| `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

```java
JSONParser parser = new JSONParser();
String stringToParse = "hello world";
JSONObject json = (JSONObject) parser.parse(stringToParse);
String encodedKey = "testCypher";

String payload = pubnub.encrypt(json, encodedKey);
```

## Encrypt file input stream

Encrypts input stream with a cipher key.

### Method(s)

```java
pubnub.encryptInputStream(inputStream, cipherKey)
```

| Parameter | Description |
| --- | --- |
| `inputStream` *Type: InputStreamDefault: n/a | Stream with content to encrypt. |
| `cipherKey`Type: StringDefault: `PNConfiguration.getCipherKey()` | Cipher key used for encryption. |

### Sample code

```java
pubnub.encryptInputStream(InputStream inputStream);
pubnub.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.

```java
pubnub.decrypt(data, customCipherKey)
```

| Parameter | Description |
| --- | --- |
| `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)

```java
pubnub.decryptInputStream(inputStream, cipherKey)
```

| Parameter | Description |
| --- | --- |
| `inputStream` *Type: InputStreamDefault: n/a | Stream with content encrypted data. |
| `cipherKey`Type: StringDefault: `PNConfiguration.getCipherKey()` | Cipher key used for encryption. |

### Sample code

```java
FileInputStream encryptedInputStream = new FileInputStream("./cat_picture_encrypted.jpg");
InputStream 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:

```java
public final List<String> getSubscribedChannelGroups()
```

### Sample code

#### Get subscribed channel groups

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

### Response

`List<String>`

```json
["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:

```java
public final List<String> getSubscribedChannels()
```

### Sample code

#### Get subscribed channels

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

### Response

`List<String>`

```json
["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.

```java
disconnect()
```

This method doesn't take any arguments.

### Sample code

```java
pubnub.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.

```java
reconnect()
```

This method doesn't take any arguments.

### Sample code

```java
pubnub.reconnect();
```