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

Add Device to Channel

import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.v2.PNConfiguration
import com.pubnub.api.enums.PNLogVerbosity
import com.pubnub.api.enums.PNPushEnvironment
import com.pubnub.api.enums.PNPushType
import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper
import com.pubnub.api.models.consumer.push.PNPushAddChannelResult
import com.pubnub.api.models.consumer.push.PNPushListProvisionsResult
import com.pubnub.api.models.consumer.push.PNPushRemoveChannelResult
import com.pubnub.api.models.consumer.push.PNPushRemoveAllChannelsResult
import com.pubnub.api.models.consumer.PNPublishResult

/**
* This example demonstrates how to use Mobile Push Notifications in the PubNub Kotlin SDK.
show all 342 lines

Returns

The addPushNotificationsOnChannels() does not return actionable data, be sure to check the status object on the outcome of the operation by checking the status.error.

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

// for FCM/GCM
pubnub.auditPushChannelProvisions(
pushType = PNPushType.FCM,
deviceId = "googleDevice"
).async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}

// for APNS2
pubnub.auditPushChannelProvisions(
pushType = PNPushType.APNS2,
show all 25 lines

Returns

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

MethodTypeDescription
channels
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

// for FCM/GCM
pubnub.removePushNotificationsFromChannels(
deviceId = "googleDevice",
channels = listOf("ch1", "ch2", "ch3"),
pushType = PNPushType.FCM
).async { result -> }

// for APNS2
pubnub.removePushNotificationsFromChannels(
deviceId = "appleDevice",
channels = listOf("ch1", "ch2", "ch3"),
pushType = PNPushType.APNS2,
topic = "myapptopic",
environment = PNPushEnvironment.DEVELOPMENT
).async { result -> }

Returns

The removePushNotificationsFromChannels() does not return actionable data, be sure to check the status object on the outcome of the operation by checking the status.error.

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

// for FCM/GCM
pubnub.removeAllPushNotificationsFromDeviceWithPushToken(
deviceId = "googleDevice"
pushType = PNPushType.FCM
).async { result -> }

// for APNS2
pubnub.removeAllPushNotificationsFromDeviceWithPushToken(
deviceId = "appleDevice"
pushType = PNPushType.APNS2,
topic = "myapptopic",
environment = PNPushEnvironment.DEVELOPMENT
).async { result -> }

Returns

The removeAllPushNotificationsFromDeviceWithPushToken() does not return actionable data, be sure to check the status object on the outcome of the operation by checking the status.error.

Last updated on