Mobile Push Notifications API for 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.

Mobile Push Notifications feature enables developers to bridge native PubNub publishing with 3rd-party push notification services including Google Android FCM (Firebase Cloud Messaging) and Apple iOS APNs (Apple Push Notification service).

By using the Mobile Push Notifications feature, developers can eliminate the need for developing, configuring, and maintaining additional server-side components for third-party push notification providers.

To learn more, read about Mobile Push Notifications.

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
}
}

Add Device to Channel

Requires Mobile Push Notifications add-on

This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Enable mobile push notifications on provided set of channels.

Method(s)

To run Adding Device to Channel you can use the following method(s) in the Kotlin SDK:

pubnub.addPushNotificationsOnChannels(
pushType: PNPushType.FCM,
channels: List<String>,
deviceId: String,
topic: String,
environment: PNPushEnvironment
).async { result -> }
* required
ParameterDescription
pushType *
Type: PNPushType
Accepted values: PNPushType.FCM, PNPushType.APNS2.
channels *
Type: List<String>
Add mobile push notifications on the specified channels.
deviceId *
Type: String
The device ID (token) to associate with mobile push notifications.
topic
Type: String
Notifications topic name (usually it is bundle identifier of application for Apple platform). Required only if pushType set to APNS2.
environment
Type: PNPushEnvironment
Environment within which device should manage list of channels with enabled notifications (works only if pushType set to APNS2).

Basic Usage

Reference code

This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.


Returns

The addPushNotificationsOnChannels() does not return actionable data, be sure to check result object on the outcome of the operation by checking the result.isFailure or handling exception result.onFailure(exception -> { }).

List Channels For Device

Requires Mobile Push Notifications add-on

This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Request for all channels on which push notification has been enabled using specified pushToken.

Method(s)

To run Listing Channels For Device you can use the following method(s) in the Kotlin SDK:

pubnub.auditPushChannelProvisions(
pushType: PNPushType,
deviceId: String,
topic: String,
environment: PNPushEnvironment
).async { result, status }
* required
ParameterDescription
pushType *
Type: PNPushType
Accepted values: PNPushType.FCM, PNPushType.APNS2.
deviceId *
Type: String
The device ID (token) to associate with mobile push notifications.
topic
Type: String
Notifications topic name (usually the application bundle identifier for Apple platform). Required only if pushType set to APNS2.
environment
Type: PNPushEnvironment
Environment within which device should manage list of channels with enabled notifications (works only if pushType set to APNS2).

Basic Usage

List Channels For Device


Returns

The auditPushChannelProvisions() operation returns a PNPushListProvisionsResult? which contains the following operations:

MethodDescription
channels
Type: List<String>
List of channels associated for mobile push notifications.

Remove Device From Channel

Requires Mobile Push Notifications add-on

This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Disable mobile push notifications on provided set of channels.

Method(s)

To Removing Device From Channel you can use the following method(s) in the Kotlin SDK:

pubnub.removePushNotificationsFromChannels(
pushType: PNPushType,
channels: List<String>,
deviceId: String,
topic: String,
environment: PNPushEnvironment
).async { result -> }
* required
ParameterDescription
pushType *
Type: PNPushType
Accepted values: PNPushType.FCM, PNPushType.APNS2.
channels *
Type: List<String>
Remove mobile push notifications from the specified channels.
deviceId *
Type: String
The device ID (token) to associate with mobile push notifications.
topic
Type: String
Notifications topic name (usually the application bundle identifier for Apple platform). Required only if pushType set to APNS2.
environment
Type: PNPushEnvironment
Environment within which device should manage list of channels with enabled notifications (works only if pushType set to APNS2).

Basic Usage

Remove Device From Channel


Returns

The removePushNotificationsFromChannels() does not return actionable data, be sure to check result object on the outcome of the operation by checking the result.isFailure or handling exception result.onFailure(exception -> { }).

Remove all mobile push notifications

Requires Mobile Push Notifications add-on

This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Disable mobile push notifications from all channels registered with the specified pushToken.

Method(s)

To Remove all mobile push notifications you can use the following method(s) in the Kotlin SDK:

pubnub.removeAllPushNotificationsFromDeviceWithPushToken(
pushType: PNPushType,
deviceId: String,
topic: String,
environment: PNPushEnvironment
).async { result -> }
* required
ParameterDescription
pushType *
Type: PNPushType
Accepted values: PNPushType.FCM, PNPushType.APNS2.
deviceId *
Type: String
The device ID (token) to associate with mobile push notifications.
topic
Type: String
Notifications topic name (usually the application bundle identifier for Apple platform). Required only if pushType set to APNS2.
environment
Type: PNPushEnvironment
Environment within which device should manage list of channels with enabled notifications (works only if pushType set to APNS2).

Basic Usage

Remove all mobile push notifications


Returns

The removeAllPushNotificationsFromDeviceWithPushToken() does not return actionable data, be sure to check the status object on the outcome of the operation by checking the be sure to check result object on the outcome of the operation by checking the result.isFailure or handling exception result.onFailure(exception -> { }).

Last updated on