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.

The Mobile Push Notifications feature connects native PubNub publishing to third-party push services. Supported services include Google Android FCM (Firebase Cloud Messaging) and Apple iOS APNs (Apple Push Notification service).

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 a device to a push notifications channel

Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Enable mobile push notifications on a set of channels.

Method(s)

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>
Channels to enable for push notifications.
deviceId *
Type: String
Device ID (push token).
topic
Type: String
APNs topic (bundle identifier). Required for APNS2.
environment
Type: PNPushEnvironment
APNs environment (APNS2).

Sample code

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

No payload is returned. Check result.isFailure on error or handle exceptions via result.onFailure { ... }.

List push notifications channels for a device

Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Get all channels with push notifications for the specified push token.

Method(s)

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
Device ID (push token).
topic
Type: String
APNs topic (bundle identifier). Required for APNS2.
environment
Type: PNPushEnvironment
APNs environment (APNS2).

Sample code

List channels for device


Returns

Returns PNPushListProvisionsResult? with:

MethodDescription
channels
Type: List<String>
Channels associated with push notifications.

Remove a device from push notifications channels

Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Disable push notifications on selected channels.

Method(s)

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>
Channels to disable for push notifications.
deviceId *
Type: String
Device ID (push token).
topic
Type: String
APNs topic (bundle identifier). Required for APNS2.
environment
Type: PNPushEnvironment
APNs environment (APNS2).

Sample code

Remove device from channel


Returns

No payload is returned. Check result.isFailure on error or handle exceptions via result.onFailure { ... }.

Remove a device from all push notifications channels

Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Disable push notifications from all channels registered for the specified push token.

Method(s)

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
Device ID (push token).
topic
Type: String
APNs topic (bundle identifier). Required for APNS2.
environment
Type: PNPushEnvironment
APNs environment (APNS2).

Sample code

Remove all mobile push notifications


Returns

No payload is returned. Check result.isFailure on error or handle exceptions via result.onFailure { ... }.

Last updated on