Swift Native Mobile Push API Reference for Realtime Apps
Note
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.
Adding Device to APNS2 Channels
Requires Mobile Push Notifications add-on Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description _
Data Yes The list of channels to add the device registration to. device
Data Yes The device to add/remove from the channels. on
String Yes The topic of the remote notification (which is typically the bundle ID for your app). environment
PubNub.PushEnvironment Yes .development
The APS environment to register the device. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success An
Array
of channels added for notifications on a specific device token. - Failure An
Error
describing the failure.
- Success An
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 Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description for
Data Yes The device token used during registration. on
String Yes The topic of the remote notification (which is typically the bundle ID for your app). environment
PubNub.PushEnvironment Yes .development
The APS environment to register the device. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success An
Array
of channels added for notifications on a specific device token. - Failure An
Error
describing the failure.
- Success An
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 Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description _
Data Yes The list of channels to disable registration device
Data Yes The device to add/remove from the channels. on
String Yes The topic of the remote notification (which is typically the bundle ID for your app). environment
PubNub.PushEnvironment Yes .development
The APS environment to register the device. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success An
Array
of channels disabled from notifications on a specific device token. - Failure An
Error
describing the failure.
- Success An
Basic Usage
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 Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description for
Data Yes The device token used during registration. on
String Yes The topic of the remote notification (which is typically the bundle ID for your app). environment
PubNub.PushEnvironment Yes .development
The APS environment to register the device. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success A
Void
indicating a success. - Failure An
Error
describing the failure.
- Success A
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 Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
Enable push notifications on provided set of channels. (This method is deprecated in favor of the corresponding APNS2 method above. Use only for legacy APNS 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)?)
Parameter Type Required Defaults Description _
[String] Yes The list of channels to add the device registration to. for
Data Yes A device token to identify the device for registration changes. of
PubNub.PushService Yes .apns
The type of Remote Notification service used to send the notifications. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success An
Array
of channels added for notifications on a specific device token. - Failure An
Error
describing the failure.
- Success An
Basic Usage
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 Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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 APNS2 method above. Use only for legacy APNS 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)?)
Parameter Type Required Defaults Description for
Data Yes A device token to identify the device for registration changes. of
PubNub.PushService Yes .apns
The type of Remote Notification service used to send the notifications. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success An
Array
of channels added for notifications on a specific device token. - Failure An
Error
describing the failure.
- Success An
Basic Usage
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 Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
Disable push notifications on provided set of channels. (This method is deprecated in favor of the corresponding APNS2 method above. Use only for legacy APNS 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)?)
Parameter Type Required Defaults Description _
[String] Yes The list of channels to remove the device registration from. for
Data Yes A device token to identify the device for registration changes. of
PubNub.PushService Yes .apns
The type of Remote Notification service used to send the notifications. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success An
Array
of channels added for notifications on a specific device token. - Failure An
Error
describing the failure.
- Success An
Basic Usage
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 Requires that you enable the Mobile Push Notifications for your key. Refer to the following page for details on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
Disable push notifications from all channels registered with the specified pushToken. (This method is deprecated in favor of the corresponding APNS2 method above. Use only for legacy APNS 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)?)
Parameter Type Required Defaults Description for
Data Yes A device token to identify the device for registration changes. of
PubNub.PushService Yes .apns
The type of Remote Notification service used to send the notifications. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<Void, Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success A
Void
indicating a success. - Failure An
Error
describing the failure.
- Success A
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)")
}
}