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), Apple iOS APNs (Apple Push Notification service), and Microsoft Windows Phone MPNS (Microsoft 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
    byRemovingDataYesThe list of channels to remove the device registration from.
    thenAddingDataYesThe 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).
    environmentPushRouter.EnvironmentYes.developmentThe APS environment to register the device.
Adding Device to Channel:
pubnub.modifyAPNSDevicesOnChannels(
  byRemoving: [],
  thenAdding: ["channelSwift"],
  for: deviceToken, 
  on: "com.app.bundle", 
  environment: .production
 ) { result in
  switch result {
  case let .success(response):
    print("Successful Push Modification Response: \(response)")
  case let .failure(error):
    print("Failed Push List Response: \(error.localizedDescription)")
  }
}

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

public struct ModifiedPushChannelsPayloadResponse: Codable {
  /// Response message
  public let message: EndpointResponseMessage = .acknowledge
  
  /// Channels that had push support added
  public let added: [String]
  
  /// Channels that had push support removed
  public let removed: [String]
  
  /// All channels that were modified
  public var channels: [String] {
    return added + removed
  }
}

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
    for deviceTokenDataYesThe device token used during registration.
    on topicStringYesThe topic of the remote notification (which is typically the bundle ID for your app).
    environmentPushRouter.EnvironmentYes.developmentThe APS environment to register the device.
pubnub.listAPNSChannelsOnDevice(for: deviceToken, on: "com.app.bundle", environment: .production) { result in
  switch result {
  case let .success(response):
    print("Successful Push List Response: \(response)")
  case let .failure(error):
    print("Failed Push List Response: \(error.localizedDescription)")
  }
}
Response objects which is returned by client when APNS List Devices API is used:
public struct RegisteredPushChannelsPayloadResponse: Codable {
  public let channels: [String]
}

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
    byRemovingDataYesThe list of channels to remove the device registration from.
    thenAddingDataYesThe list of channels to add the device registration to.
    forDataYesThe device token used during registration.
    onStringYesThe topic of the remote notification (which is typically the bundle ID for your app).
    environmentPushRouter.EnvironmentYes.developmentThe APS environment to register the device.
pubnub.modifyAPNSDevicesOnChannels(
  byRemoving: ["channelSwift"],
  thenAdding: [],
  for: deviceToken, 
  on: "com.app.bundle", 
  environment: .production
 ) { result in
  switch result {
  case let .success(response):
    print("Successful Push Modification Response: \(response)")
  case let .failure(error):
    print("Failed Push List Response: \(error.localizedDescription)")
  }
}
Response objects which is returned by client when APNS Remove Device API is used:
public struct ModifiedPushChannelsPayloadResponse: Codable {
  /// Response message
  public let message: EndpointResponseMessage = .acknowledge
  
  /// Channels that had push support added
  public let added: [String]
  
  /// Channels that had push support removed
  public let removed: [String]
  
  /// All channels that were modified
  public var channels: [String] {
    return added + removed
  }
}

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).
    environmentPushRouter.EnvironmentYes.developmentThe APS environment to register the device.
pubnub.removeAPNSPushDevice(
  for: deviceToken, 
  on: "com.app.bundle", 
  environment: .production
 ) { result in
  switch result {
  case let .success(response):
    print("Successful Push Deletion Response: \(response)")
  case let .failure(error):
    print("Failed Push List Response: \(error.localizedDescription)")
  }
}
Response objects which is returned by client when APNS Remove All Devices API is used:
public struct ModifiedPushChannelsPayloadResponse: Codable {
  /// Response message
  public let message: EndpointResponseMessage = .acknowledge
  
  /// Channels that had push support added
  public let added: [String]
  
  /// Channels that had push support removed
  public let removed: [String]
  
  /// All channels that were modified
  public var channels: [String] {
    return added + removed
  }
}

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
    byRemoving[String]Yes The list of channels to remove the device registration from.
    thenAdding[String]Yes The list of channels to add the device registration to.
    forDataYes A device token to identify the device for registration changes.
    ofEndpoint.PushTypeNo.apnsThe type of Remote Notification service used to send the notifications. Available types include:
    • .apns - Apple Push Notification Service (APNS) for Apple iOS
    • .mpns - Microsoft Push Notification Service (MPNS) for Microsoft Windows Phone
    • .gcm - Google Cloud Messaging (GCM) / Firebase Cloud Messaging (FCM) for Google Android
pubnub.modifyPushChannelRegistrations(
  byRemoving: [],
  thenAdding: ["channelSwift"],
  for: deviceToken
) { result in
  switch result {
  case let .success(response):
    print("Successful Push Modification Response: \(response)")
  case let .failure(error):
    print("Failed Push Modification Response: \(error.localizedDescription)")
  }
}

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

GenericServicePayloadResponse(
  message: EndpointResponseMessage.acknowledge, 
  details: [], 
  service: "push", 
  status: 200, 
  error: false, 
  channels: [:]
)

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
    forDataYes A device token to identify the device for registration changes.
    ofEndpoint.PushTypeNo.apnsThe type of Remote Notification service used to send the notifications. Available types include:
    • .apns - Apple Push Notification Service (APNS) for Apple iOS
    • .mpns - Microsoft Push Notification Service (MPNS) for Microsoft Windows Phone
    • .gcm - Google Cloud Messaging (GCM) / Firebase Cloud Messaging (FCM) for Google Android
pubnub.listPushChannelRegistrations(for: deviceToken) { result in
  switch result {
  case let .success(response):
    print("Successful Push List Response: \(response)")
  case let .failure(error):
    print("Failed Push List Response: \(error.localizedDescription)")
  }
}
Response objects which is returned by client when APNS List Devices API is used:
public struct RegisteredPushChannelsPayloadResponse: Codable {
  let channels: [String]
}

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
    byRemoving[String]YesThe list of channels to remove the device registration from.
    thenAdding[String]YesThe list of channels to add the device registration to.
    forDataYesA device token to identify the device for registration changes.
    ofEndpoint.PushTypeNo.apnsThe type of Remote Notification service used to send the notifications. Available types include:
    • .apns - Apple Push Notification Service (APNS) for Apple iOS
    • .mpns - Microsoft Push Notification Service (MPNS) for Microsoft Windows Phone
    • .gcm - Google Cloud Messaging (GCM) / Firebase Cloud Messaging (FCM) for Google Android
pubnub.modifyPushChannelRegistrations(
  byRemoving: ["channelSwift"],
  thenAdding: [],
  for: deviceToken
) { result in
  switch result {
  case let .success(response):
    print("Successful Push Modification Response: \(response)")
  case let .failure(error):
    print("Failed Push Modification Response: \(error.localizedDescription)")
  }
}
Response objects which is returned by client when APNS Remove Device API is used:
GenericServicePayloadResponse(
  message: EndpointResponseMessage.acknowledge, 
  details: [], 
  service: "push", 
  status: 200, 
  error: false, 
  channels: [:]
)

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.
    ofEndpoint.PushTypeNo.apnsThe type of Remote Notification service used to send the notifications. Available types include:
    • .apns - Apple Push Notification Service (APNS) for Apple iOS
    • .mpns - Microsoft Push Notification Service (MPNS) for Microsoft Windows Phone
    • .gcm - Google Cloud Messaging (GCM) / Firebase Cloud Messaging (FCM) for Google Android
pubnub.removeAllPushChannelRegistrations(for: deviceToken) { result in
  switch result {
  case let .success(response):
    print("Successful Push Deletion Response: \(response)")
  case let .failure(error):
    print("Failed Push Deletion Response: \(error.localizedDescription)")
  }
}
Response objects which is returned by client when APNS Remove All Devices API is used:
GenericServicePayloadResponse(
  message: EndpointResponseMessage.acknowledge, 
  details: [], 
  service: "push", 
  status: 200, 
  error: false, 
  channels: [:]
)