Delivery callbacks (iOS Native)
Each notification triggered through Sortment carries a callback URL in its payload. To track delivery and interaction, your app reads that URL and sends a POST request with the relevant status (RECEIVED or CLICKED) back to it.
On a native iOS app this is wired up in two places:
A Notification Service Extension reports
RECEIVEDwhenever a notification is delivered, in every app state.Your
AppDelegatereportsCLICKEDwhen the user taps a notification.
Both read the callback URL from the same place in the payload, using the shared helper shown below.
Setup: register the notification delegate
Set your AppDelegate as the notification center delegate when the app launches. Without this, the tap callback will not fire.
import UIKit
import UserNotifications
import FirebaseCore
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
application.registerForRemoteNotifications()
return true
}
}1. Notification Delivered (RECEIVED)
Delivery is tracked with a Notification Service Extension. It runs in its own process, launched by the system before the notification is displayed, so it reports delivery in every app state: foreground, background, and closed (including force-quit).
Step 1: Add the extension target
In Xcode, go to File → New → Target, select Notification Service Extension, give it a name (for example, NotificationReceiptService), choose Swift, and click Finish. If prompted to activate the scheme, click Cancel.
Make sure the extension's deployment target is the same as or lower than your main app's.
Step 2: Add the receipt logic
Open the generated NotificationService.swift and replace its contents with the following.
2. Notification Clicked (CLICKED)
When the user taps a notification to open the app, from either the background or a closed state, iOS routes both cases through userNotificationCenter(_:didReceive:withCompletionHandler:). Add this method to your AppDelegate.
The callbackUrl(from:) helper is the same as the one in the extension. It is repeated here because the extension and the app are separate targets and do not share code.
Last updated