Push Notifications API for PubNub Swift Native SDK

Migration guide

The PubNub Swift 3.0 SDK contains many significant changes from the 2.x SDK, including breaking changes. Please refer to the PubNub Swift 3.0 Migration Guide for more details.

PubNub's Mobile Push Gateway 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 Gateway, 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 Push Notifications.

Adding Device to APNs2 Channels

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.

Description

Enable APNs2 push notifications on provided set of channels.

Method(s)

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

addAPNSDevicesOnChannels(  _ additions: [String],  device token: Data,  on topic: String,  environment: PushEnvironment = .development,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
_DataYesThe list of channels to add the device registration to.
deviceDataYesThe device to add/remove from the channels.
onStringYesThe topic of the remote notification (which is typically the bundle ID for your app).
environmentPubNub.PushEnvironmentYes.developmentThe APS environment to register the device.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

An Array of channels added for notifications on a specific device token.

Failure

An Error describing the failure.

Basic Usage

Adding Device to Channel:

pubnub.addAPNSDevicesOnChannels(
["channelSwift"],
for: deviceToken,
on: "com.app.bundle",
environment: .production
) { result in
switch result {
case let .success(channels):
print("The list of channels added for push: \(channels)")
case let .failure(error):
print("Failed Push List Response: \(error.localizedDescription)")
}
}

Listing APNs2 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.

Description

Request for all channels on which APNs2 push notification has been enabled using specified device token and topic.

Method(s)

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

listAPNSPushChannelRegistrations(  for deviceToken: Data,  on topic: String,  environment: PushEnvironment = .development,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
forDataYesThe device token used during registration.
onStringYesThe topic of the remote notification (which is typically the bundle ID for your app).
environmentPubNub.PushEnvironmentYes.developmentThe APS environment to register the device.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

An Array of channels added for notifications on a specific device token.

Failure

An Error describing the failure.

Basic Usage

Listing APNs2 Channels For Device

pubnub.listAPNSPushChannelRegistrations(
for: deviceToken,
on: "com.app.bundle",
environment: .production
) { result in
switch result {
case let .success(channels):
print("The list of channels enabled for push: \(channels)")
case let .failure(error):
print("Failed Push List Response: \(error.localizedDescription)")
}
}

Removing Device From APNs2 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.

Description

Disable push notifications on provided set of channels.

Method(s)

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

removeAPNSDevicesOnChannels(  _ removals: [String],  device token: Data,  on topic: String,  environment: PushEnvironment = .development,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
_DataYesThe list of channels to disable registration
deviceDataYesThe device to add/remove from the channels.
onStringYesThe topic of the remote notification (which is typically the bundle ID for your app).
environmentPubNub.PushEnvironmentYes.developmentThe APS environment to register the device.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

An Array of channels disabled from notifications on a specific device token.

Failure

An Error describing the failure.

Basic Usage

Removing Device From Channel

pubnub.removeAPNSDevicesOnChannels(
["channelSwift"],
for: deviceToken,
on: "com.app.bundle",
environment: .production
) { result in
switch result {
case let .success(channels):
print("The list of channels disabled for push: \(channels)")
case let .failure(error):
print("Failed Push List Response: \(error.localizedDescription)")
}
}

Remove all APNs2 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.

Description

Disable APNs2 push notifications from all channels which is registered with specified pushToken.

Method(s)

To run Remove all APNs2 push notifications you can use the following method(s) in the Swift SDK:

removeAllAPNSPushDevice(  for deviceToken: Data,  on topic: String,  environment: PushEnvironment = .development,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<Void, Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
forDataYesThe device token used during registration.
onStringYesThe topic of the remote notification (which is typically the bundle ID for your app).
environmentPubNub.PushEnvironmentYes.developmentThe APS environment to register the device.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Voidindicating a success.

Failure

An Error describing the failure.

Basic Usage

Remove all push notifications

pubnub.removeAllAPNSPushDevice(
for: deviceToken,
on: "com.app.bundle",
environment: .production
) { result in
switch result {
case let .success:
print("All channels have been removed for the device token.")
case let .failure(error):
print("Failed Push List Response: \(error.localizedDescription)")
}
}

Adding Device to Channel (deprecated)

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.

Description

Enable push notifications on provided set of channels. This method is deprecated in favor of the corresponding HTTP/2-based APNs method above. Use only for legacy binary APNs interface publishing.

Method(s)

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

addPushChannelRegistrations(  _ additions: [String],  for deviceToken: Data,  of pushType: PushService = .apns,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
_[String]YesThe list of channels to add the device registration to.
forDataYesA device token to identify the device for registration changes.
ofPubNub.PushServiceYes.apnsThe type of Remote Notification service used to send the notifications.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

An Array of channels added for notifications on a specific device token.

Failure

An Error describing the failure.

Basic Usage

Adding Device to Channel

pubnub.addPushChannelRegistrations(
["channelSwift"],
for: deviceToken
) { result in
switch result {
case let .success(channels):
print("The list of channels added for push: \(channels)")
case let .failure(error):
print("Failed Push Modification Response: \(error.localizedDescription)")
}
}

Listing Channels For Device (deprecated)

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.

Description

Request for all channels on which push notification has been enabled using specified pushToken. This method is deprecated in favor of the corresponding HTTP/2-based APNs method above. Use only for legacy binary APNs interface publishing.

Method(s)

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

listPushChannelRegistrations(  for deviceToken: Data,  of pushType: PushService = .apns,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
forDataYesA device token to identify the device for registration changes.
ofPubNub.PushServiceYes.apnsThe type of Remote Notification service used to send the notifications.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

An Array of channels added for notifications on a specific device token.

Failure

An Error describing the failure.

Basic Usage

isting Channels For Device

pubnub.listPushChannelRegistrations(for: deviceToken) { result in
switch result {
case let .success(channels):
print("The list of channels enabled for push: \(channels)")
case let .failure(error):
print("Failed Push List Response: \(error.localizedDescription)")
}
}

Removing Device From Channel (deprecated)

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.

Description

Disable push notifications on provided set of channels. This method is deprecated in favor of the corresponding HTTP/2-based APNs method above. Use only for legacy binary APNs interface publishing.

Method(s)

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

removePushChannelRegistrations(  _ removals: [String],  for deviceToken: Data,  of pushType: PushService = .apns,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
_[String]YesThe list of channels to remove the device registration from.
forDataYesA device token to identify the device for registration changes.
ofPubNub.PushServiceYes.apnsThe type of Remote Notification service used to send the notifications.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

An Array of channels added for notifications on a specific device token.

Failure

An Error describing the failure.

Basic Usage

Removing Device From Channel

pubnub.removePushChannelRegistrations(
["channelSwift"],
for: deviceToken
) { result in
switch result {
case let .success(channels):
print("The list of channels disabled for push: \(channels)")
case let .failure(error):
print("Failed Push Modification Response: \(error.localizedDescription)")
}
}

Remove all push notifications (deprecated)

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.

Description

Disable push notifications from all channels registered with the specified pushToken. This method is deprecated in favor of the corresponding HTTP/2-based APNs method above. Use only for legacy binary APNs interface publishing.

Method(s)

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

removeAllPushChannelRegistrations(  for deviceToken: Data,  of pushType: PushService = .apns,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<Void, Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
forDataYesA device token to identify the device for registration changes.
ofPubNub.PushServiceYes.apnsThe type of Remote Notification service used to send the notifications.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<Void, Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Voidindicating a success.

Failure

An Error describing the failure.

Basic Usage

Remove all push notifications

pubnub.removeAllPushChannelRegistrations(for: deviceToken) { result in
switch result {
case let .success:
print("All channels have been removed for the device token.")
case let .failure(error):
print("Failed Push Deletion Response: \(error.localizedDescription)")
}
}
Last updated on