Mobile Push Notifications API for Swift Native 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.

Add 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.

Enable APNs2 mobile 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)?`
)
* required
ParameterDescription
_ *
Type: Data
Default:
n/a
The list of channels to add the device registration to.
device *
Type: Data
Default:
n/a
The device to add/remove from the channels.
on *
Type: String
Default:
n/a
The topic of the remote notification (which is typically the bundle ID for your app).
environment *
Type: PubNub.PushEnvironment
Default:
.development
The APS environment to register the device.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<[String], Error>) -> Void)?
Default:
nil
The 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

Reference code example

This example is a self-contained code snippet ready to run in an SDK's IDE. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Adding Device to Channel:

import PubNubSDK

// Sample device token
let deviceToken = Data([0x01, 0x02, 0x03, 0x04]) // Replace with actual device token

// Creates a configuration object using the demo keys
let config = PubNubConfiguration(
publishKey: "demo",
subscribeKey: "demo",
userId: "myUniqueUserId"
)

// Initializes a PubNub object with the configuration
let pubnub = PubNub(configuration: config)

show all 31 lines

List 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.

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)?`
)
* required
ParameterDescription
for *
Type: Data
Default:
n/a
The device token used during registration.
on *
Type: String
Default:
n/a
The topic of the remote notification (which is typically the bundle ID for your app).
environment *
Type: PubNub.PushEnvironment
Default:
.development
The APS environment to register the device.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<[String], Error>) -> Void)?
Default:
nil
The 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

List 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)")
}
}

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

Disable mobile 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)?`
)
* required
ParameterDescription
_ *
Type: Data
Default:
n/a
The list of channels to disable registration
device *
Type: Data
Default:
n/a
The device to add/remove from the channels.
on *
Type: String
Default:
n/a
The topic of the remote notification (which is typically the bundle ID for your app).
environment *
Type: PubNub.PushEnvironment
Default:
.development
The APS environment to register the device.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<[String], Error>) -> Void)?
Default:
nil
The 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

Remove 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 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 APNs2 mobile push notifications from all channels which is registered with specified pushToken.

Method(s)

To run Remove all APNs2 mobile 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)?
)
* required
ParameterDescription
for *
Type: Data
Default:
n/a
The device token used during registration.
on *
Type: String
Default:
n/a
The topic of the remote notification (which is typically the bundle ID for your app).
environment *
Type: PubNub.PushEnvironment
Default:
.development
The APS environment to register the device.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<[String], Error>) -> Void)?
Default:
nil
The async Result of the method call

Completion Handler Result

Success

A Voidindicating a success.

Failure

An Error describing the failure.

Basic Usage

Remove all mobile 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)")
}
}

Add 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.

Enable mobile 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)?`
)
* required
ParameterDescription
_ *
Type: [String]
Default:
n/a
The list of channels to add the device registration to.
for *
Type: Data
Default:
n/a
A device token to identify the device for registration changes.
of *
Type: PubNub.PushService
Default:
.apns
The type of Remote Notification service used to send the notifications.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<[String], Error>) -> Void)?
Default:
nil
The 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

Add 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)")
}
}

List 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.

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)?`
)
* required
ParameterDescription
for *
Type: Data
Default:
n/a
A device token to identify the device for registration changes.
of *
Type: PubNub.PushService
Default:
.apns
The type of Remote Notification service used to send the notifications.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<[String], Error>) -> Void)?
Default:
nil
The 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 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)")
}
}

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

Disable mobile 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)?`
)
* required
ParameterDescription
_ *
Type: [String]
Default:
n/a
The list of channels to remove the device registration from.
for *
Type: Data
Default:
n/a
A device token to identify the device for registration changes.
of *
Type: PubNub.PushService
Default:
.apns
The type of Remote Notification service used to send the notifications.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<[String], Error>) -> Void)?
Default:
nil
The 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

Remove 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 mobile 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.

Disable mobile 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 mobile 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)?
)
* required
ParameterDescription
for *
Type: Data
Default:
n/a
A device token to identify the device for registration changes.
of *
Type: PubNub.PushService
Default:
.apns
The type of Remote Notification service used to send the notifications.
custom
Type: RequestConfiguration
Default:
RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session
completion
Type: ((Result<Void, Error>) -> Void)?
Default:
nil
The async Result of the method call

Completion Handler Result

Success

A Voidindicating a success.

Failure

An Error describing the failure.

Basic Usage

Remove all mobile 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