SwiftSwift Native Mobile Push API Reference for Realtime Apps

 
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 GCM (Google Cloud Messaging)/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.

Learn more about our Mobile Push Gateway here.


Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
Enable APNS2 push notifications on provided set of channels.
To run Adding Device to APNS2 Channel you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    _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.
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)")
  }
}

Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
Request for all channels on which APNS2 push notification has been enabled using specified device token and topic.
To run Listing APNS2 Channels For Device you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}

Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.

Disable push notifications on provided set of channels.

To run Removing Device From APNS2 Channel you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    _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.
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)")
  }
}

Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
Disable APNS2 push notifications from all channels which is registered with specified pushToken.
To run Remove all APNS2 push notifications you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}

Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
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.)
To run Adding Device to Channel you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    _[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.
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)")
  }
}

Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
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.)
To run Listing Channels For Device you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}

Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
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.)
To run Removing Device From Channel you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    _[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.
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)")
  }
}

Requires Mobile Push Notifications add-on XRequires 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:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
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.)
To run Remove all push notifications you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}