Utility Methods API for PubNub Kotlin SDK
Breaking changes in v9.0.0
PubNub Kotlin SDK version 9.0.0 unifies the codebases for Kotlin and Java 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 Kotlin 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.
Request execution
Most PubNub Kotlin SDK method invocations return an Endpoint object, which allows you to decide whether to perform the operation synchronously or asynchronously.
You must invoke the .sync()
or .async()
method on the Endpoint to execute the request, or the operation will not be performed.
val channel = pubnub.channel("channelName")
channel.publish("This SDK rules!").async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}
Create Push Payload
This method creates the push payload for use in the appropriate endpoint calls.
Method(s)
val pushPayloadHelper = PushPayloadHelper()
val fcmPayload = FCMPayload()
val apnsPayload = APNSPayload()
pushPayloadHelper.fcmPayload = fcmPayload
pushPayloadHelper.apnsPayload = apnsPayload
Parameter | Type | Required | Description |
---|---|---|---|
apnsPayload | APNSPayload | Optional | Set APNS and APNS2 Payload. Associated devices will receive only the data supplied here within the pn_apns key. |
fcmPayload | FCMPayload | Optional | Set FCM Payload. Associated devices will receive only the data supplied here within the pn_gcm key. |
commonPayload | Map<String, Any> | 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, Any> | Yes | Builds the payload from the values set using the parameters. |
Basic Usage
Create Push Payload
// Create an instance of PushPayloadHelper
val pushPayloadHelper = PushPayloadHelper()
// Setup FCM parameters (FCMPayload)
val fcmPayload = FCMPayload().apply {
notification = FCMPayload.Notification().apply {
title = "Notification title"
body = "Notification body text"
image = "http://example.com/image.png"
}
data = mapOf(
"city" to "sf",
"count" to 71,
"is_private" to true
)
show all 74 linesResponse
The PushPayloadHelper#build()
operation returns a Map<String, Any>
which can be passed directly as the message
parameter to the pubnub.publish()
method.
Encrypt
This function allow 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 Kotlin SDK.
pubnub.encrypt(
inputString: String,
cipherKey: String
)
Parameter | Type | Required | Description |
---|---|---|---|
inputString | String | Yes | The data to encrypt. |
cipherKey | 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
val aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule(cipherKey = "myCipherKey01", randomIv = true)
val stringToBeEncrypted = "string to be encrypted"
val encryptedData = aesCbcCryptoModule.encrypt(stringToBeEncrypted.toByteArray())
Returns
It returns the encrypted inputString
as a String
.
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.
Method(s)
pubnub.encryptInputStream(inputStream: InputStream, cipherKey: String)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
inputStream | InputStream | Yes | Stream with content to encrypt. | |
cipherKey | String | Optional | PNConfiguration.getCipherKey() | 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
val aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule(cipherKey = "myCipherKey01", randomIv = true)
val encryptedStream = aesCbcCryptoModule.encryptStream(stringToBeEncrypted.byteInputStream())
Returns
InputStream with encrypted data.
Decrypt
This function allow to decrypt
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)
pubnub.decrypt(
inputString: String,
cipherKey: String
)
To decrypt the data you can use the following method(s) in Kotlin SDK.
Parameter | Type | Required | Description |
---|---|---|---|
inputString | String | Yes | The data to decrypt. |
cipherKey | String | Optional | Cipher key to use for decryption. |
Basic Usage
val aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule(cipherKey = "myCipherKey01", randomIv = true)
val stringToBeEncrypted = "string to be encrypted"
val encryptedData = aesCbcCryptoModule.encrypt(stringToBeEncrypted.toByteArray())
val decryptedData = aesCbcCryptoModule.decrypt(encryptedData)
Returns
It returns the decrypted inputString
as a String
.
Decrypt File Input Stream
Method(s)
Decrypts 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.
pubnub.decryptInputStream(inputStream: InputStream, cipherKey: String)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
inputStream | InputStream | Yes | Stream with content encrypted data. | |
cipherKey | String | Optional | PNConfiguration.getCipherKey() | 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
val aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule(cipherKey = "myCipherKey01", randomIv = true)
val encryptedStream = aesCbcCryptoModule.encryptStream(stringToBeEncrypted.byteInputStream())
val decryptedStream = aesCbcCryptoModule.decryptStream(encryptedStream)
Returns
InputStream with decrypted data.
Destroy
Destroy frees up the threads and allows for clean exit.
Method(s)
destroy()
Basic Usage
pubnub.destroy()
Returns
None
Get Subscribed Channel Groups
Returns all the subscribed channel groups in a List<String>
.
Method(s)
To Get Subscribe Channel Groups you can use the following method(s) in the Kotlin SDK:
fun getSubscribedChannelGroups(): List<String>
Basic Usage
val subscribedChannels = 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 Kotlin 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 Kotlin 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 Kotlin SDK:
time()
Basic Usage
Get PubNub Timetoken
pubnub.time()
.async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}
Returns
The time()
operation returns a PNTimeResult?
which contains the following operations:
Method | Type | Description |
---|---|---|
timetoken | Long | Returns a long representation of current timetoken. |