Swift Client SDK

Quickstart

  • Swift
  • Objective-C
import UIKit
import PushNotifications
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 
    var window: UIWindow?
    let pushNotifications = PushNotifications.shared
 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.pushNotifications.start(instanceId: "YOUR_INSTANCE_ID")
        self.pushNotifications.registerForRemoteNotifications()
        return true
    }
 
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        self.pushNotifications.registerDeviceToken(deviceToken) {
            try? self.pushNotifications.subscribe(interest: "hello")
        }   
    }
 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        self.pushNotifications.handleNotification(userInfo: userInfo)
    }
}

Initialization

The minimum you need to initialize a client is the following:

1
let pushNotifications = PushNotifications.shared

Registration

Register with the Beams service.

This must be done in application:didFinishLaunchingWithOptions: delegate method in AppDelegate class.

1
self.pushNotifications.start(instanceId: "YOUR_INSTANCE_ID")

Register to Receive Remote Notifications

This is a convenience method that uses alert, sound, and badge as default authorization options.

1
self.pushNotifications.registerForRemoteNotifications()

You can specify constants to request authorization for multiple items.

1
func registerForRemoteNotifications(options:)

iOS

Available options: UNAuthorizationOptions

macOS

Available options: NSApplication.RemoteNotificationType

Register Device Token

Register device token with the Beams service.

1
self.pushNotifications.registerDeviceToken(deviceToken)

Subscribe

Subscribe to an interest hello.

1
try? self.pushNotifications.subscribe(interest: "hello")

Set Subscriptions

Sets the subscriptions state for the device. Any interests not in the set will be unsubscribed from, so this will replace the interest set by the one provided.

1
try? self.pushNotifications.setSubscriptions(interests: ["donuts", "pizza", "gaming"])

Unsubscribe

Unsubscribe from an interest hello.

1
try? self.pushNotifications.unsubscribe(interest: "hello")

Unsubscribe All

Unsubscribe from all the interests.

1
self.pushNotifications.unsubscribeAll()

Get Interests

Get a list of all the interests.

1
self.pushNotifications.getInterests()

Handle Notification

Use this method to enable Pusher related features, for example, the notification Insights.

1
self.pushNotifications.handleNotification(userInfo: userInfo)

We provide an option to ignore Pusher related remote notifications.

1
2
3
4
let remoteNotificationType = self.pushNotifications.handleNotification(userInfo: userInfo)
if remoteNotificationType == .ShouldIgnore {
    return
}

Interests Changed Delegate

Method interestsSetDidChange(interests:) of the protocol InterestsChangedDelegate allows the delegate to be informed when interests set changes.

import UIKit
import Foundation
import PushNotifications
 
class ViewController: UIViewController, InterestsChangedDelegate {
    let pushNotifications = PushNotifications.shared
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.pushNotifications.delegate = self
    }
 
    func interestsSetDidChange(interests: [String]) {
        print(interests) // ["vegan-pizza", "donuts"]
    }
}

Did you find this document useful?

We are always striving to create the most accurate and informative docs as possible. If there is something especially wrong (or right) here then please let us know.