Push notifications
Overview
The following guide covers the configuration of the iOS SDK for processing incoming push notifications and sending extracted attribution data to AppsFlyer.
There are 2 methods of implementing the integration:
- By utilizing OneLink in the push payload (recommended method). Step 3 is required only if implementing this solution.
- By using plain JSON in the push payload (legacy method).
Choose the right method for you based on how the marketer structures the push notification.
Integrating AppsFlyer with iOS push notifications
Integrating AppsFlyer with iOS push notifications consists of the following:
- Configuring the AppsFlyer SDK.
- Configuring a
UNUserNotificationCenter
delegate.
Prerequisites
Before you continue, make sure you have:
- An iOS app with push notifications enabled.
- Integrated the SDK.
- If implementing the recommended OneLink-based solution, you need the name of the key inside the push notification payload that contains the OneLink (provided by the app marketer).
Steps
-
Configure the app to use the
UNUserNotificationCenter
delegate:if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: { _, _ in } ) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() }
-
Implement the
UNUserNotificationCenter
delegate. In thedidReceive
method, callhandlePushNotification
:@available(iOS 10, *) extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo print(userInfo) completionHandler() AppsFlyerLib.shared().handlePushNotification(userInfo) } // Receive displayed notifications for iOS 10 devices. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo print(userInfo) // Change this to your preferred presentation option completionHandler([[.alert, .sound]]) } }
-
This step is required only if you're implementing the recommended OneLink-based solution.
IndidFinishLaunchingWithOptions
, calladdPushNotificationDeepLinkPath
before callingstart
:AppsFlyerLib.shared().addPushNotificationDeepLinkPath(["af_push_link"])
In this example, the SDK is configured to look for the
af_push_link
key in the push notification payload.
When callingaddPushNotificationDeepLinkPath
the SDK verifies that:- The required key exists in the payload.
- The key contains a valid OneLink URL.
Note
addPushNotificationDeepLinkPath
accepts an array of strings too, to allow you to extract the relevant key from nested JSON structures. For more information, seeaddPushNotificationDeepLinkPath
.
Updated 11 months ago