Mobile Push Notifications API for PubNub Cocoa Swift SDK

This SDK has been replaced by a new PubNub Swift SDK written purely in Swift. Check it out here

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.

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 Swift SDK:

open func addPushNotificationsOnChannels(
_ channels: [String],
withDevicePushToken pushToken: Data,
andCompletion closure: PubNub.PNPushNotificationsStateModificationCompletionBlock? = nil
)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which mobile push notifications should be enabled.
pushTokenDataYesDevice push token which should be used to enable mobile push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoAdd mobile push notifications process completion closure which has one argument - request processing status to report about whether data push was successful or not (errorData contains error information in case of failure).

Basic Usage

Add Device to Channel

self.client.addPushNotificationsOnChannels(["wwdc", "google.io"],
withDevicePushToken: self.devicePushToken,
andCompletion: { (status) in

if !status.isError {

// Handle successful push notification enabling on passed channels.
}
else {

/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.
show all 20 lines

Response

Response objects which is returned by client when APNS Add Device API is used:

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Add Device to Channel (Builder Pattern)

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 Swift SDK.

APNS Token

push().enable()
.channels([String])
.apnsToken(Data)
.environment(PNAPNSProduction)
.topic(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which mobile push notifications should be enabled.
apnsTokenDataYesAPNS-provided device push token which should be used to enable push notifications on specified set of channels.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications addition on channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token

push().enable()
.channels([String])
.fcmToken(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which mobile push notifications should be enabled.
fcmTokenStringYesFCM-provided device push token which should be used to enable push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications addition on channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS2 Token

self.client.push().enable().channels(["channel1", "channel2"])
.apnsToken(self.pushToken)
.environment(PNAPNSProduction)
.topic("myapptopic")
.performWithCompletion({ (status) in
if !status.isError {
// Handle successful push notification enabling on passed channels.
} else {
/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry()
show all 18 lines

FCM Token

self.client.push().enable().channels(["channel1", "channel2"])
.fcmToken(self.pushToken)
.performWithCompletion({ (status) in
if !status.isError {
// Handle successful push notification enabling on passed channels.
} else {
/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry()
*/
}
show all 16 lines

Response

Response objects which is returned by client when APNS Add Device API is used:

open class PNAcknowledgmentStatus : PNErrorStatus {

}

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 Swift SDK:

open func pushNotificationEnabledChannelsForDeviceWithPushToken(
_ pushToken: Data,
andCompletion closure: PubNub.PNPushNotificationsStateAuditCompletionBlock
)
ParameterTypeRequiredDescription
pushTokenDataYesDevice push token.
closurePNPushNotificationsStateAuditCompletionBlockYesThe completion closure which will be called when the processing is complete, has two arguments: result - in case of successful processing (data will contain results of mobile push notifications audit operation); status - in case of error while processing (errorDatacontains error information).

Basic Usage

List Channels For Device

self.client.pushNotificationEnabledChannelsForDeviceWithPushToken(self.devicePushToken,
andCompletion: { (result, status) in

if status == nil {

// Handle downloaded list of channels using: result.data.channels
}
else {

/**
Handle audition error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

show all 19 lines

Response

Response objects which is returned by client when APNS List Devices API is used:

open class PNAPNSEnabledChannelsData : PNServiceData {

// Channels with active mobile push notifications.
open var channels: [String] { get }
}

open class PNAPNSEnabledChannelsResult : PNResult {

// Stores reference on APNS enabled channels audit request processing information.
open var data: PNAPNSEnabledChannelsData { get }
}

List Channels For Device (Builder Pattern)

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 Swift SDK.

APNS Token

push().audit()
.apnsToken(Data)
.environment(PNAPNSEnvironment)
.topic(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
apnsTokenDataYesAPNS-provided device push token against which search on PubNub service should be performed.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateAuditCompletionBlockYesPush notifications status processing completion closure which pass two arguments: result - in case of successful request processing data field will contain results of push notifications audit operation; status - in case if error occurred during request processing.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token

push().audit()
.fcmToken(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
fcmTokenStringYesFCM-provided device push token against which search on PubNub service should be performed.
closurePNPushNotificationsStateAuditCompletionBlockYesPush notifications status processing completion closure which pass two arguments: result - in case of successful request processing data field will contain results of push notifications audit operation; status - in case if error occurred during request processing.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS2 Token

self.client.push().audit().apnsToken(self.pushToken)
.environment(PNAPNSProduction)
.topic("myapptopic")
.performWithCompletion({ (result, status) in
if status == nil {
// Handle downloaded list of channels using: result.data.channels
} else {
/**
Handle audition error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry();
*/
show all 17 lines

FCM Token

self.client.push().audit().fcmToken(self.pushToken)
.performWithCompletion({ (result, status) in
if status == nil {
// Handle downloaded list of channels using: result.data.channels
} else {
/**
Handle audition error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry();
*/
}
}];

Response

Response objects which is returned by client when APNS List Devices API is used:

open class PNAPNSEnabledChannelsData : PNServiceData {

// Channels with active mobile push notifications.
open var channels: [String] { get }
}

open class PNAPNSEnabledChannelsResult : PNResult {

// Stores reference on APNS enabled channels audit request processing information.
open var data: PNAPNSEnabledChannelsData { get }
}

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 run Removing Device From Channel you can use the following method(s) in the Swift SDK:

open func removePushNotificationsFromChannels(
_ channels: [String],
withDevicePushToken pushToken: Data,
andCompletion closure: PubNub.PNPushNotificationsStateModificationCompletionBlock? = nil
)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which mobile push notifications should be disabled.If passed list is empty all notifications will be disabled.
pushTokenDataYesDevice push token which should be used to disable push notifications on specified set of channels.
closurePNChannelGroupChangeCompletionBlockNoThe completion closure which will be called when the processing is complete, has one argument: request processing status - in case of error while processing (errorDatacontains error information).

Basic Usage

Remove Device From Channel

self.client.removePushNotificationsFromChannels(["wwdc","google.io"],
withDevicePushToken: self.devicePushToken,
andCompletion: { (status) in

if !status.isError {

// Handle successful push notification disabling on passed channels.
}
else {

/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.
show all 20 lines

Response

Response objects which is returned by client when APNS Remove Device API is used:

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Remove Device From Channel (Builder Pattern)

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 run Removing Device From Channel you can use the following method(s) in the Swift SDK

APNS2 Token

push().disable()
.channels([String])
.apnsToken(Data)
.environment(PNAPNSEnvironment)
.topic(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which mobile push notifications should be disabled. If passed list is empty all notifications will be disabled.
apnsTokenDataYesAPNS-provided device push token which should be used to disable push notifications on specified set of channels.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token

push().disable()
.channels([String])
.fcmToken(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be disabled. If passed list is empty all notifications will be disabled.
fcmTokenStringYesFCM-provided device push token which should be used to disable push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS Token

self.client.push().disable().channels(["channel1", "channel2"])
.apnsToken(self.pushToken)
.environment(PNAPNSProduction)
.topic("myapptopic")
.performWithCompletion({ (status) in
if !status.isError {
// Handle successful push notification disabling on passed channels.
} else {
/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry();
show all 18 lines

FCM Token

self.client.push().disable().channels(["channel1", "channel2"])
.fcmToken(self.pushToken)
.performWithCompletion({ (status) in
if !status.isError {
// Handle successful push notification disabling on passed channels.
} else {
/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry();
*/
}
show all 16 lines

Response

Response objects which is returned by client when APNS Remove Device API is used:

open class PNAcknowledgmentStatus : PNErrorStatus {

}

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 run Remove all mobile push notifications, you can use the following method(s) in the Swift SDK:

open func removeAllPushNotificationsFromDeviceWithPushToken(
_ pushToken: Data,
andCompletion closure: PubNub.PNPushNotificationsStateModificationCompletionBlock? = nil
)
ParameterTypeRequiredDescription
pushTokenDataYesDevice push token which should be used to disable all mobile push notifications registered with it.
closurePNPushNotificationsStateModificationCompletionBlockNoThe completion closure which will be called when the processing is complete, has one argument: request processing status - in case of error while processing (errorDatacontains error information).

Basic Usage

Remove all mobile push notifications

self.client.removeAllPushNotificationsFromDeviceWithPushToken(self.devicePushToken,
andCompletion: { (status) in

if !status.isError {

/**
Handle successful push notification disabling for all channels associated with
specified device push token.
*/
}
else {

/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
show all 22 lines

Response

Response objects which is returned by client when APNS Remove All Devices API is used:

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Remove all mobile push notifications (Builder Pattern)

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 run Remove all mobile push notifications, you can use the following method(s) in the Swift SDK:

APNS2 Token

push().disable()
.channels([String])
.apnsToken(Data)
.environment(PNAPNSEnvironment)
.topic(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which mobile push notifications should be disabled. If passed list is empty all notifications will be disabled.
apnsTokenDataYesAPNS-provided device push token which should be used to disable push notifications on specified set of channels.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token

push().disable()
.channels([String])
.fcmToken(String)
.performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be disabled. If passed list is empty all notifications will be disabled.
fcmTokenStringYesFCM-provided device push token which should be used to disable push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Optional arguments

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS2 Token

self.client.push().disable().channels(["channel1", "channel2"])
.apnsToken(self.pushToken)
.environment(PNAPNSProduction)
.topic("myapptopic")
.performWithCompletion({ (status) in
if !status.isError {
// Handle successful push notification disabling on passed channels.
} else {
/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry();
show all 18 lines

FCM Token

self.client.push().disable().channels(["channel1", "channel2"])
.fcmToken(self.pushToken)
.performWithCompletion({ (status) in
if !status.isError {
// Handle successful push notification disabling on passed channels.
} else {
/**
Handle modification error. Check 'category' property
to find out possible reason because of which request did fail.
Review 'errorData' property (which has PNErrorData data type) of status
object to get additional information about issue.

Request can be resent using: status.retry();
*/
}
show all 16 lines

Response

open class PNAcknowledgmentStatus : PNErrorStatus {

}
Last updated on