Push Notifications

Overview

The OpenWeb SDK can deliver push notifications to re-engage users when new interactions occur on their comments while the app is in the background.

Setup

Info.plist Configuration

Add the following entries to your app's Info.plist:

<key>UIBackgroundModes</key>
<array>
  <string>fetch</string>
</array>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
  <string>com.openweb.sdk.background-fetch</string>
</array>
  • UIBackgroundModes - must include fetch to allow the SDK to perform background fetches.
  • BGTaskSchedulerPermittedIdentifiers - must include com.openweb.sdk.background-fetch, the
    task identifier the SDK registers with the system.
📘

The SDK validates these entries on startup when push notifications are enabled in your spot configuration. If either is missing, an error will be logged.


Request Permission to Send Notifications

Request the user's authorization to display notifications by calling requestAuthorization(options:completionHandler:).

UNUserNotificationCenter.current()
    .requestAuthorization(options: [.alert, .sound]) { granted, error in
    // handle result
    if let error {
        print("Authorization failed: \(error.localizedDescription)")
        return
    }
    print(granted ? "Permission granted" : "Permission denied")
}

Handling a Notification Tap

When a user taps an OpenWeb notification, forward it to the SDK to find out which Conversation to open, then open it.

Set the Notification Delegate

Set your UNUserNotificationCenterDelegate synchronously in application(_:didFinishLaunchingWithOptions:) so that taps which cold-launch the app are handled.

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

Open the Conversation

In userNotificationCenter(_:didReceive:withCompletionHandler:), pass the tapped response to OWHelpers.getRoutingData(notificationResponse:). It returns nil for any notification that did not originate from the OpenWeb SDK, so it is safe to call on every notification tap. On a non-nil result, pass routing.postId and routing.route straight to the Conversation open API.

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

        let manager: OWManagerProtocol = OpenWeb.manager
        let helpers: OWHelpers = manager.helpers

        // Returns nil for any notification that isn't an OpenWeb notification
        guard let routing = helpers.getRoutingData(notificationResponse: response) else { return }

        // `article` and `additionalSettings` are the same values you already use to open a Conversation.
        manager.ui.components.openConversation(
            postId: routing.postId,
            article: article,
            route: routing.route,
            presentationalMode: .present(viewController: presenter, style: .pageSheet),
            additionalSettings: additionalSettings
        ) { result in
            if case .failure(let error) = result {
                // Handle the error / Show appropriate user UI
            }
        }
    }
}
📘

These are local notifications generated by the SDK, so you do not need application(_:didReceiveRemoteNotification:) or remote push-token registration.

For details on getRoutingData and OWRoutingData, see OWHelpers - Notification Routing.



Did this page help you?