Utility Methods API for PubNub Android SDK
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)
PushPayloadHelper()
.setApnsPayload(PushPayloadHelper.APNSPayload)
.setFcmPayload(PushPayloadHelper.FCMPayload)
.setCommonPayload(Map<String, Object>)
.build()
Parameter | Type | Required | 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_gcm key. |
setCommonPayload() | Map<String, Object> | Optional | Set common Payload. Native PubNub subscribers will receive the data provided here, together with the pn_apns , and pn_gcm objects. |
build() | Map<String, Object> | Yes | Builds the payload from the values set using the parameters. |
Basic Usage
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);
show all 82 linesResponse
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)
destroy()
Basic Usage
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.
pubnub.encrypt(data, customCipherKey)
Parameter | Type | Required | Description |
---|---|---|---|
data | String | Yes | The data to encrypt . |
customCipherKey | String | Optional | If it's not provided, the cipher key from config will be used. |
Basic Usage
Encrypt part of message
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)
pubnub.encryptInputStream(inputStream, cipherKey)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
inputStream | InputStream | Yes | Stream with content to encrypt. | |
cipherKey | String | Optional | PNConfiguration.getCipherKey() | Cipher key used for encryption. |
Basic Usage
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.
pubnub.decrypt(data, customCipherKey)
Parameter | Type | Required | Description |
---|---|---|---|
data | String | Yes | The data to decrypt . |
customCipherKey | String | Optional | 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)
pubnub.decryptInputStream(inputStream, cipherKey)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
inputStream | InputStream | Yes | Stream with content encrypted data. | |
cipherKey | String | Optional | PNConfiguration.getCipherKey() | Cipher key used for encryption. |
Basic Usage
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:
public final List<String> getSubscribedChannelGroups()
Basic Usage
Get Subscribed Channel Groups
List<String> groups = pubnub.getSubscribedChannelGroups();
Response
List<String>
["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:
public final List<String> getSubscribedChannels()
Basic Usage
Get Subscribed Channels
List<String> channels = pubnub.getSubscribedChannels();
Response
List<String>
["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.
disconnect()
This method doesn't take any arguments.
Basic Usage
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.
reconnect()
This method doesn't take any arguments.
Basic Usage
pubnub.reconnect();
Time
This function will return a 17 digit precision Unix epoch.
Algorithm constructing the timetoken
timetoken = (Unix epoch time in seconds) * 10000000
Example of creating a timetoken for a specific time and date:
08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000
Method(s)
To fetch Time
you can use the following method(s) in Android SDK:
this.pubnub.Time()
Parameter | Type | Required | Description |
---|---|---|---|
async | PNCallback | Yes | PNCallback of type PNTimeResult |
Basic Usage
Get PubNub Timetoken
pubnub.time().async(new PNCallback<PNTimeResult>() {
@Override
public void onResponse(PNTimeResult result, PNStatus status) {
// handle time result.
}
});
Returns
The time()
operation returns a PNTimeResult
which contains the following operations:
Method | Type | Description |
---|---|---|
getTimetoken() | Long | Returns a long representation of current timetoken. |