PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

›API Reference

cocoa-Swift

  • Getting Started
  • API Reference

    • Configuration
    • Publish & Subscribe
    • Presence
    • Access Manager
    • Channel Groups
    • Message Persistence
    • Mobile Push
    • Message Actions
    • Miscellaneous
  • Status Events
  • Troubleshooting
  • Change Log
  • Feature Support
  • Platform Support

Push Notifications API for PubNub Cocoa Swift SDK

This SDK has been replaced by a new PubNub Swift SDK written purely in Swift. Check it out here

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

Enable push notifications on provided set of channels.

Method(s)

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

open func addPushNotificationsOnChannels(_ channels: [String], withDevicePushToken pushToken: Data, andCompletion closure: PubNub.PNPushNotificationsStateModificationCompletionBlock? = nil)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be enabled.
pushTokenDataYesDevice push token which should be used to enable push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoAdd push notifications process completion closure which has one argument - request processing status to report about whether data push was successful or not (errorData contains error information in case of failure).

Basic Usage

Adding Device to Channel:

self.client.addPushNotificationsOnChannels(["wwdc", "google.io"],
                                            withDevicePushToken: self.devicePushToken,
                                            andCompletion: { (status) in

    if !status.isError {

        // Handle successful push notification enabling on passed channels.
    }
    else {

        /**
         Handle modification error. Check 'category' property
         to find out possible reason because of which request did fail.
         Review 'errorData' property (which has PNErrorData data type) of status
         object to get additional information about issue.

         Request can be resent using: status.retry()
         */
    }
})

Response

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

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Adding Device to Channel (Builder Pattern)

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.

Method(s)

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

APNS Token:

push().enable().channels([String]).apnsToken(Data).environment(PNAPNSProduction).topic(String)performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be enabled.
apnsTokenDataYesAPNS-provided device push token which should be used to enabled push notifications on specified set of channels.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications addition on channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token:

push().enable().channels([String]).fcmToken(String).performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be enabled.
fcmTokenStringYesFCM-provided device push token which should be used to enabled push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications addition on channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS2 Token:

self.client.push().enable().channels(["channel1", "channel2"])
    .apnsToken(self.pushToken)
    .environment(PNAPNSProduction)
    .topic("myapptopic")
    .performWithCompletion({ (status) in
        if !status.isError {
           // Handle successful push notification enabling on passed channels.
        } else {
           /**
            Handle modification error. Check 'category' property
            to find out possible reason because of which request did fail.
            Review 'errorData' property (which has PNErrorData data type) of status
            object to get additional information about issue.

            Request can be resent using: status.retry()
            */
        }
    });

FCM Token:

self.client.push().enable().channels(["channel1", "channel2"])
    .fcmToken(self.pushToken)
    .performWithCompletion({ (status) in
        if !status.isError {
           // Handle successful push notification enabling on passed channels.
        } else {
           /**
            Handle modification error. Check 'category' property
            to find out possible reason because of which request did fail.
            Review 'errorData' property (which has PNErrorData data type) of status
            object to get additional information about issue.

            Request can be resent using: status.retry()
            */
        }
    });

Response

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

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Listing 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 push notification has been enabled using specified pushToken.

Method(s)

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

open func pushNotificationEnabledChannelsForDeviceWithPushToken(_ pushToken: Data, andCompletion closure: PubNub.PNPushNotificationsStateAuditCompletionBlock)
ParameterTypeRequiredDescription
pushTokenDataYesDevice push token.
closurePNPushNotificationsStateAuditCompletionBlockYesThe completion closure which will be called when the processing is complete, has two arguments: result - in case of successful processing (data will contain results of push notifications audit operation); status - in case of error while processing (errorDatacontains error information).

Basic Usage

Listing Channels For Device:

self.client.pushNotificationEnabledChannelsForDeviceWithPushToken(self.devicePushToken,
                                                                  andCompletion: { (result, status) in

    if status == nil {

        // Handle downloaded list of channels using: result.data.channels
    }
    else {

        /**
         Handle audition error. Check 'category' property
         to find out possible reason because of which request did fail.
         Review 'errorData' property (which has PNErrorData data type) of status
         object to get additional information about issue.

         Request can be resent using: status.retry()
         */
    }
})

Response

Response objects which is returned by client when APNS List Devices API is used:

open class PNAPNSEnabledChannelsData : PNServiceData {

    // Channels with active push notifications.
    open var channels: [String] { get }
}

open class PNAPNSEnabledChannelsResult : PNResult {

    // Stores reference on APNS enabled channels audit request processing information.
    open var data: PNAPNSEnabledChannelsData { get }
}

Listing Channels For Device (Builder Pattern)

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.

Method(s)

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

APNS Token:

push().audit().apnsToken(Data).environment(PNAPNSEnvironment).topic(String).performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
apnsTokenDataYesAPNS-provided device push token against which search on PubNub service should be performed.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateAuditCompletionBlockYesPush notifications status processing completion closure which pass two arguments: result - in case of successful request processing data field will contain results of push notifications audit operation; status - in case if error occurred during request processing.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token:

push().audit().fcmToken(String).performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
fcmTokenStringYesFCM-provided device push token against which search on PubNub service should be performed.
closurePNPushNotificationsStateAuditCompletionBlockYesPush notifications status processing completion closure which pass two arguments: result - in case of successful request processing data field will contain results of push notifications audit operation; status - in case if error occurred during request processing.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS2 Token:

self.client.push().audit().apnsToken(self.pushToken)
    .environment(PNAPNSProduction)
    .topic("myapptopic")
    .performWithCompletion({ (result, status) in
        if status == nil {
            // Handle downloaded list of channels using: result.data.channels
        } else {
            /**
             Handle audition error. Check 'category' property
             to find out possible reason because of which request did fail.
             Review 'errorData' property (which has PNErrorData data type) of status
             object to get additional information about issue.

             Request can be resent using: status.retry();
            */
        }
    }];

FCM Token:

self.client.push().audit().fcmToken(self.pushToken)
    .performWithCompletion({ (result, status) in
        if status == nil {
            // Handle downloaded list of channels using: result.data.channels
        } else {
            /**
             Handle audition error. Check 'category' property
             to find out possible reason because of which request did fail.
             Review 'errorData' property (which has PNErrorData data type) of status
             object to get additional information about issue.

             Request can be resent using: status.retry();
            */
        }
    }];

Response

Response objects which is returned by client when APNS List Devices API is used:

open class PNAPNSEnabledChannelsData : PNServiceData {

    // Channels with active push notifications.
    open var channels: [String] { get }
}

open class PNAPNSEnabledChannelsResult : PNResult {

    // Stores reference on APNS enabled channels audit request processing information.
    open var data: PNAPNSEnabledChannelsData { get }
}

Removing Device From 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 Channel you can use the following method(s) in the Swift SDK:

open func removePushNotificationsFromChannels(_ channels: [String], withDevicePushToken pushToken: Data, andCompletion closure: PubNub.PNPushNotificationsStateModificationCompletionBlock? = nil)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be disabled.If passed list is empty all notifications will be disabled.
pushTokenDataYesDevice push token which should be used to disable push notifications on specified set of channels.
closurePNChannelGroupChangeCompletionBlockNoThe completion closure which will be called when the processing is complete, has one argument: request processing status - in case of error while processing (errorDatacontains error information).

Basic Usage

Removing Device From Channel:

self.client.removePushNotificationsFromChannels(["wwdc","google.io"],
                                                withDevicePushToken: self.devicePushToken,
                                                andCompletion: { (status) in

    if !status.isError {

        // Handle successful push notification disabling on passed channels.
    }
    else {

        /**
         Handle modification error. Check 'category' property
         to find out possible reason because of which request did fail.
         Review 'errorData' property (which has PNErrorData data type) of status
         object to get additional information about issue.

         Request can be resent using: status.retry();
         */
    }
})

Response

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

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Removing Device From Channel (Builder Pattern)

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 Channel you can use the following method(s) in the Swift SDK
APNS2 Token:

push().disable().channels([String]).apnsToken(Data).environment(PNAPNSEnvironment).topic(String).performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be disabled. If passed list is empty all notifications will be disabled.
apnsTokenDataYesAPNS-provided device push token which should be used to disable push notifications on specified set of channels.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token:

push().disable().channels([String]).fcmToken(String).performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be disabled. If passed list is empty all notifications will be disabled.
fcmTokenStringYesFCM-provided device push token which should be used to disable push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS Token:

self.client.push().disable().channels(["channel1", "channel2"])
    .apnsToken(self.pushToken)
    .environment(PNAPNSProduction)
    .topic("myapptopic")
    .performWithCompletion({ (status) in
        if !status.isError {
           // Handle successful push notification disabling on passed channels.
        } else {
           /**
            Handle modification error. Check 'category' property
            to find out possible reason because of which request did fail.
            Review 'errorData' property (which has PNErrorData data type) of status
            object to get additional information about issue.

            Request can be resent using: status.retry();
            */
        }
    });

FCM Token:

self.client.push().disable().channels(["channel1", "channel2"])
    .fcmToken(self.pushToken)
    .performWithCompletion({ (status) in
        if !status.isError {
           // Handle successful push notification disabling on passed channels.
        } else {
           /**
            Handle modification error. Check 'category' property
            to find out possible reason because of which request did fail.
            Review 'errorData' property (which has PNErrorData data type) of status
            object to get additional information about issue.

            Request can be resent using: status.retry();
            */
        }
    });

Response

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

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Remove all 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 push notifications from all channels registered with the specified pushToken.

Method(s)

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

open func removeAllPushNotificationsFromDeviceWithPushToken(_ pushToken: Data, andCompletion closure: PubNub.PNPushNotificationsStateModificationCompletionBlock? = nil)
ParameterTypeRequiredDescription
pushTokenDataYesDevice push token which should be used to disable all push notifications registered with it.
closurePNPushNotificationsStateModificationCompletionBlockNoThe completion closure which will be called when the processing is complete, has one argument: request processing status - in case of error while processing (errorDatacontains error information).

Basic Usage

Remove all push notifications:

self.client.removeAllPushNotificationsFromDeviceWithPushToken(self.devicePushToken,
                                                              andCompletion: { (status) in

    if !status.isError {

        /**
         Handle successful push notification disabling for all channels associated with
         specified device push token.
         */
    }
    else {

        /**
         Handle modification error. Check 'category' property
         to find out possible reason because of which request did fail.
         Review 'errorData' property (which has PNErrorData data type) of status
         object to get additional information about issue.

         Request can be resent using: status.retry();
         */
    }
})

Response

Response objects which is returned by client when APNS Remove All Devices API is used:

open class PNAcknowledgmentStatus : PNErrorStatus {

}

Remove all push notifications (Builder Pattern)

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.

Method(s)

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

APNS2 Token:

push().disable().channels([String]).apnsToken(Data).environment(PNAPNSEnvironment).topic(String).performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be disabled. If passed list is empty all notifications will be disabled.
apnsTokenDataYesAPNS-provided device push token which should be used to disable push notifications on specified set of channels.
topicStringYesNotifications topic name (usually it is application's bundle identifier).
environmentPNAPNSEnvironmentYesOne of PNAPNSEnvironment fields which specify environment within which device should manage list of channels with enabled notifications (works only APNs2).
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

FCM Token:

push().disable().channels([String]).fcmToken(String).performWithCompletion(PNPushNotificationsStateModificationCompletionBlock)
ParameterTypeRequiredDescription
channels[String]YesList of channel names for which push notifications should be disabled. If passed list is empty all notifications will be disabled.
fcmTokenStringYesFCM-provided device push token which should be used to disable push notifications on specified set of channels.
closurePNPushNotificationsStateModificationCompletionBlockNoPush notifications removal from channels processing completion closure which pass only one argument - request processing status to report about how data pushing was successful or not.
Note

This method uses the builder pattern, you can remove the arguments which are optional.

Basic Usage

APNS2 Token:

self.client.push().disable().channels(["channel1", "channel2"])
    .apnsToken(self.pushToken)
    .environment(PNAPNSProduction)
    .topic("myapptopic")
    .performWithCompletion({ (status) in
        if !status.isError {
           // Handle successful push notification disabling on passed channels.
        } else {
           /**
            Handle modification error. Check 'category' property
            to find out possible reason because of which request did fail.
            Review 'errorData' property (which has PNErrorData data type) of status
            object to get additional information about issue.

            Request can be resent using: status.retry();
            */
        }
    });

FCM Token:

self.client.push().disable().channels(["channel1", "channel2"])
    .fcmToken(self.pushToken)
    .performWithCompletion({ (status) in
        if !status.isError {
           // Handle successful push notification disabling on passed channels.
        } else {
           /**
            Handle modification error. Check 'category' property
            to find out possible reason because of which request did fail.
            Review 'errorData' property (which has PNErrorData data type) of status
            object to get additional information about issue.

            Request can be resent using: status.retry();
            */
        }
    });

Response

open class PNAcknowledgmentStatus : PNErrorStatus {

} 
← Message PersistenceMessage Actions →
  • Adding Device to Channel
    • Description
    • Method(s)
    • Basic Usage
    • Response
  • Adding Device to Channel (Builder Pattern)
    • Description
    • Method(s)
    • Basic Usage
    • Response
  • Listing Channels For Device
    • Description
    • Method(s)
    • Basic Usage
    • Response
  • Listing Channels For Device (Builder Pattern)
    • Description
    • Method(s)
    • Basic Usage
    • Response
  • Removing Device From Channel
    • Description
    • Method(s)
    • Basic Usage
    • Response
  • Removing Device From Channel (Builder Pattern)
    • Description
    • Method(s)
    • Basic Usage
    • Response
  • Remove all push notifications
    • Description
    • Method(s)
    • Basic Usage
    • Response
  • Remove all push notifications (Builder Pattern)
    • Description
    • Method(s)
    • Basic Usage
    • Response
© PubNub Inc. - Privacy Policy