Utility Methods API for PubNub Kotlin SDK

The methods on this page are utility methods that don't fit into other categories.

Calling Kotlin methods

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 choose one of these or the operation will not be performed at all.

For example, the following code is valid and will compile, but the publish won't be performed:

pubnub.publish(
message = "this sdk rules!",
channel = "my_channel"
)

To successfully publish a message, you must follow the actual method invocation with whether to perform it synchronously or asynchronously, for example:

pubnub.publish(
message = "this sdk rules!",
channel = "my_channel"
).async { result, status ->
if (status.error) {
// handle error
} else {
// handle successful method result
}
}

Create Push Payload

Description

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
ParameterTypeRequiredDescription
apnsPayloadAPNSPayloadOptionalSet APNS and APNS2 Payload. Associated devices will receive only the data supplied here within the pn_apns key.
fcmPayloadFCMPayloadOptionalSet FCM Payload. Associated devices will receive only the data supplied here within the pn_gcm key.
commonPayloadMap<String, Any>OptionalSet common Payload. Native PubNub subscribers will receive the data provided here, together with the pn_apns and pn_gcm objects.
buildMap<String, Any>YesBuilds 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 lines

Response

The PushPayloadHelper#build() operation returns a Map<String, Any> which can be passed directly as the message parameter to the pubnub.publish() method.

Encrypt

Description

This function allow to encrypt the data.

Method(s)

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

pubnub.encrypt(
inputString: String,
cipherKey: String
)
ParameterTypeRequiredDescription
inputStringStringYesThe data to encrypt.
cipherKeyStringOptionalIf it's not provided, the cipher key from config will be used.

Basic Usage

Encrypt part of message

val encrypted = pubnub.encrypt(inputString = unecrypted, cipherKey = "myCipherKey")

Returns

It returns the encrypted inputString as a String.

Encrypt File Input Stream

Description

Encrypts input stream with a cipher key.

Method(s)

pubnub.encryptInputStream(inputStream: InputStream, cipherKey: String)
ParameterTypeRequiredDefaultDescription
inputStreamInputStreamYesStream with content to encrypt.
cipherKeyStringOptionalPNConfiguration.getCipherKey()Cipher key used for encryption.

Basic Usage


val encrypted = pubnub.encryptInputStream(inputStream = unecrypted, cipherKey = "myCipherKey");

Returns

InputStream with encrypted data.

Decrypt

Description

This function allow to decrypt the data.

Method(s)

pubnub.decrypt(
inputString: String,
cipherKey: String
)

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

ParameterTypeRequiredDescription
inputStringStringYesThe data to decrypt.
cipherKeyStringOptionalIf it's not provided, the cipher key from config will be used.

Basic Usage

val decrypted = pubnub.decrypt(inputString = encrypted, cipherKey = "myCipherKey")

Returns

It returns the decrypted inputString as a String.

Decrypt File Input Stream

Description

Method(s)

Decrypts input stream with a cipher key.

pubnub.decryptInputStream(inputStream: InputStream, cipherKey: String)
ParameterTypeRequiredDefaultDescription
inputStreamInputStreamYesStream with content encrypted data.
cipherKeyStringOptionalPNConfiguration.getCipherKey()Cipher key used for encryption.

Basic Usage

val decryptedInputStream = pubnub.decryptInputStream(inputStream = encrypted, cipherKey = "myCipherKey")

Returns

InputStream with decrypted data.

Destroy

Description

Destroy frees up the threads and allows for clean exit.

Method(s)

destroy()

Basic Usage

pubnub.destroy()

Returns

None

Get Subscribed Channel Groups

Description

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"]

Reconnect

Description

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

Description

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, status ->
if (!status.error) {
println(result!!.timetoken)
} else {
// handle error
status.exception?.printStackTrace()
}
}

Returns

The time() operation returns a PNTimeResult? which contains the following operations:

MethodTypeDescription
timetokenLongReturns a long representation of current timetoken.
Last updated on