Push Notifications

Setting up

Unflow Push is the companion to Unflow that allows you to take your engagement to the next level. To get started you'll need your Unflow SDK client to be setup, which you can do following the instructions here.

Prerequisites

Before configuring push for Unflow, you must have configured Apple's push notification service. This is necessary as Unflow delivers push notifications directly through the push service. Full instructions on how to do so can be found in the links provided, but will also be briefly summarised below in the quickstart section.

Existing Setups

If you already use push in your iOS project, and you have your push key, then you can move on to the next section where we configure Unflow Push.

Apple Push Notifications QuickStart

Create your keys

You'll need to upload your APNs key to the Unflow Push service. To generate your key head to the Apple developer site and select keys, under Certificates, Identifiers & Profiles.

Tap the plus and set a key name, its best to make this something obvious like "Push". Select the checkbox next to "Apple Push Notifications service (APNs)" and continue, then register.

Apple's developer website with the certificates, identifiers and profiles tab open

Add capabilities

Your app will need to have the push notifications entitlement to be able to recieve push notifications. To enable this, open Xcode and head to your projects settings. Add push notifications from the "Signing & Capabilities" tab.

Xcode open with the Signing and Capabilities editor.

Configuring Unflow Push

1. Upload your keys to Unflow

You'll need to get your KeyID and TeamID from App store connects keys section, alongside the key and team ID. You'll also need your Push key, which you'll have recieved when you setup push for the first time.

Head to the push section of the Unflow dashboard and upload the file you just downloaded, enter your team ID and the key ID, then finally your bundle ID for the app you want to send push notifications to.

You're now setup to send pushes, and we'll be able to test that push is configured correctly once we have the iOS side enabled.

2. Configure the App Delegate

Next, you'll need to get your app to register with Apple's servers for push notifications, then add some code to tell Unflow about those notifications.

SwiftUI

If you're using SwiftUI, you might not have an AppDelegate. If so, you'll need to add one using the UIApplicationDelegateAdaptor helper. This is not an optional step.

An example can be found below if you need it, if not, skip ahead.

Adding an AppDelegate to SwiftUI

The SwiftUI app lifecycle does not by default have an AppDelegate. A sample is below that includes everything you need.

@main
struct Sample_AppApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

// MARK: - AppDelegate
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        return true
    }

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: nil, sessionRole: .windowApplication)
    }
}

Inside your AppDelegate, you'll need to register for push notification tokens, and pass the notifications that come in to Unflow Push.

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        // Register for push as soon as we can.
        application.registerForRemoteNotifications()
        // Tell unflow about the launch.
        UnflowSDK.client.processLaunchNotification(launchOptions: launchOptions)
        return true
    }

    // Required in our next steps
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: nil, sessionRole: .windowApplication)
    }

    // Pass the token directly to Unflow. If needed, you can pass this to other services.
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        UnflowSDK.client.registerPushToken(token: deviceToken)
    }

    // Tell Unflow that a notification was recieved.
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        let didHandle = UnflowSDK.client.didReceiveNotification(userInfo: userInfo)
        if didHandle {
            completionHandler(.newData)
        } else {
            completionHandler(.noData)
        }
    }
}

3. Configure launch notifications

In order to handle all types of notifications, you need to add some specific handling for them. This is required to make sure that when the app is launched as a result of a notification when the app is in the background, that Unflow can process it.

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        // Everything else
        // ..
        UNUserNotificationCenter.current().delegate = self
        return true
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        UnflowPush.recievedNotificationResponse(response: response)
        completionHandler()
    }
}

4. Permissions

In order for notifications to actually be recieved by the user, you'll need to ask for permissions. The best time to do this is as early as possible in your app, after a meaningful action has been performed. For example, this might be the user signing in for the first time, or reading their first article.

UNUserNotificationCenter.current().requestAuthorization(
    options: [.alert, .badge, .sound],
    completionHandler: { _, _ in }
)

5. Deregister

When a user logs out, you may wish to stop them recieving pushes. To do this you'll need to deregister the token with Unflow.

UnflowSDK.client.deregisterToken()