Utility Methods API for PubNub 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)
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.
Deprecated
The cipherKey
parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.
If you pass cipherKey
as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.
Method(s)
To encrypt
the data you can use the following method(s) in Java SDK.
pubnub.encrypt(data, customCipherKey)
Parameter | Type | Required | Description |
---|---|---|---|
data | String | Yes | The data to encrypt . |
customCipherKey | String | Optional | Cipher key to use for encryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the cryptoModule from PubNub config will be used. For more information, refer to Crypto module configuration. |
Basic Usage
Encrypt part of message
CryptoModule aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule("myCipherKey01", true);
String stringToBeEncrypted = "string to be encrypted";
byte[] encryptedData = aesCbcCryptoModule.encrypt(stringToBeEncrypted.getBytes(StandardCharsets.UTF_8));
Encrypt File Input Stream
Encrypts input stream with a cipher key.
Deprecated
The cipherKey
parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.
If you pass cipherKey
as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.