Mobile Push Notifications API for PubNub Kotlin SDK

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.

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

Add Device to Channel

note
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, status -> }
ParameterTypeRequiredDescription
pushTypePNPushTypeYesAccepted values: PNPushType.FCM, PNPushType.APNS2.
channelsList<String>YesAdd mobile push notifications on the specified channels.
deviceIdStringYesThe device ID (token) to associate with mobile push notifications.
topicStringOptionalNotifications topic name (usually it is bundle identifier of application for Apple platform). Required only if pushType set to APNS2.
environmentPNPushEnvironmentOptionalEnvironment within which device should manage list of channels with enabled notifications (works only if pushType set to APNS2).

Basic Usage

Add Device to Channel

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

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

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

note
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 }
ParameterTypeRequiredDescription
pushTypePNPushTypeYesAccepted values: PNPushType.FCM, PNPushType.APNS2.
deviceIdStringYesThe device ID (token) to associate with mobile push notifications.
topicStringOptionalNotifications topic name (usually the application bundle identifier for Apple platform). Required only if pushType set to APNS2.
environmentPNPushEnvironmentOptionalEnvironment 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, status ->
if (!status.error) {
result!!.channels // ["ch1", "ch2"]
} else {
// handle error
status.exception?.printStackTrace()
}
}

// for APNS2
pubnub.auditPushChannelProvisions(
show all 27 lines

Returns

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

MethodTypeDescription
channelsList<String>List of channels associated for mobile push notifications.

Remove Device From Channel

note
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, status -> }
ParameterTypeRequiredDescription
pushTypePNPushTypeYesAccepted values: PNPushType.FCM, PNPushType.APNS2.
channelsList<String>YesRemove mobile push notifications from the specified channels.
deviceIdStringYesThe device ID (token) to associate with mobile push notifications.
topicStringOptionalNotifications topic name (usually the application bundle identifier for Apple platform). Required only if pushType set to APNS2.
environmentPNPushEnvironmentOptionalEnvironment 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, status -> }

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

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

note
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, status -> }
ParameterTypeRequiredDescription
pushTypePNPushTypeYesAccepted values: PNPushType.FCM, PNPushType.APNS2.
deviceIdStringYesThe device ID (token) to associate with mobile push notifications.
topicStringOptionalNotifications topic name (usually the application bundle identifier for Apple platform). Required only if pushType set to APNS2.
environmentPNPushEnvironmentOptionalEnvironment 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, status -> }

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

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