Integrate iOS SDK

Our Beams service lets you subscribe iOS devices to interests. When your servers publish push notifications to interests, the iOS devices subscribed to the relevant interests will receive them. Your iOS application can subscribe to interests using our Swift SDK. This page guides you through setting up your iOS application to register, subscribe to interests, and receive push notifications pushed to those interests.

Minimum Requirements

Xcode - The easiest way to get Xcode is from the App Store, but you can also download it from developer.apple.com if you have an AppleID registered with an Apple Developer account.

Install From CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following commands:

1
gem install cocoapods

Create an empty Podfile:

1
pod init

To integrate Beams into your Xcode project using CocoaPods, specify it in your Podfile:

1
pod 'PushNotifications'

Run the following command:

1
pod install --repo-update

When installation is complete open the .xcworkspace that was created.

Install From Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

1
2
$ brew update
$ brew install carthage

To integrate Beams into your Xcode project using Carthage, specify it in your Cartfile:

1
github "pusher/push-notifications-swift"

Run carthage update to build the framework and drag the built PushNotifications.framework into your Xcode project.

Enable Capabilities

In the project navigator, select your project, and click on the Capabilities tab.Enable Push Notifications by turning the switch ON.

Register With APNs

For your iOS app to receive push notifications, it must first register the device with APNs. You should do this when the application finishes launching, i.e. in its application:didFinishLaunchingWithOptions: handler:

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

Register With Pusher

APNs will respond with a device token identifying your app instance. This device token is passed to your application with the application:didRegisterForRemoteNotificationsWithDeviceToken: method. Pusher requires the deviceToken in order to send push notifications to the app instance. Your app should register with Push Notifications, passing along its device token. Add a handler for it:

 
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        self.pushNotifications.registerDeviceToken(deviceToken)
    }
}

Subscribe to an Interest

Let's subscribe to an interest. Expand your handler to subscribe the app to the interest hello:

 
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        self.pushNotifications.registerDeviceToken(deviceToken) {
            try? self.pushNotifications.subscribe(interest: "hello")
        }   
    }
}

When your server publishes a push notification to the interest hello, it will get passed to your app. This happens as a call to application:didReceiveRemoteNotification:fetchCompletionHandler: in your AppDelegate:

 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
    }
}

Where Next?

Now that you have integrated the SDK into your iOS project why not send a notification?

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.