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:

  1. An iOS app with push notifications enabled.
  2. Integrated the SDK.
  3. 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

  1. 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()
    }
    
  2. Implement the UNUserNotificationCenter delegate. In the didReceive method, call handlePushNotification:

    @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]])
      }
    }
    
  3. This step is required only if you're implementing the recommended OneLink-based solution.
    In didFinishLaunchingWithOptions, call addPushNotificationDeepLinkPath before calling start:

    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 calling addPushNotificationDeepLinkPath 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, see addPushNotificationDeepLinkPath.