Setting up Push Campaigns for iOS

You can set up, create, send, and measure push and in-app notification campaigns in Facebook Analytics.

iOS Push Notifications Integration

This section contains documentation on how to integrate push notifications into your iOS app.

Add the following code to your app's delegate to register to receive push notifications and send the device token.

// AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                  UIUserNotificationTypeBadge |
                                                  UIUserNotificationTypeSound);
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                           categories:nil];
  [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  [[UIApplication sharedApplication] registerForRemoteNotifications];
  
  ...

  return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [FBSDKAppEvents setPushNotificationsDeviceToken:deviceToken];
}

As of version 4.20 of the Facebook SDK for iOS, the device token is uploaded to Analytics immediately. In earlier versions of the SDK, the device token was uploaded with your next event, which meant that if no events were sent after setPushNotificationsDeviceToken, then the device would never be eligible to receive push notifications.

To measure push notification opens, you need to track two different scenarios separately:

  • Open application or resume application via push notification
  • Perform action attached to a push notification
// AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  [FBSDKAppEvents logPushNotificationOpen:userInfo];
}

- (void)application:(UIApplication *)application 
handleActionWithIdentifier:(NSString *)identifier 
forRemoteNotification:(NSDictionary *)userInfo 
  completionHandler:(void (^)())completionHandler {
  [FBSDKAppEvents logPushNotificationOpen:userInfo action:identifier];
}

iOS In-App Notifications Integration

This section contains documentation on how to integrate in-app notifications into your iOS application.

To add our framework to your app:

  • Get the FBNotifications.framework from one of the following sources: CocoaPods, GitHub, the latest Facebook SDK.
  • Drag & Drop FBNotifications.framework into your project.
  • Add FBNotifications.framework to the linking phase of your app target.
  • Add MobileCoreServices.framework and ImageIO.framework to linked frameworks list.

In-app notifications are delivered in a remote notification payload to the client. You can show them directly or preload the required content (such as images) and then display it.

You can also add the ability to receive and display in-app notifications in your app delegate:

Objective-C

/// Import the following in the same source file
#import <FBNotifications/FBNotifications.h>

/// Add the following methods the implementation of application delegate

/// This method is optional if you already have push notifications set up.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions {
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:nil];
  [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  [[UIApplication sharedApplication] registerForRemoteNotifications];

  return YES;
}

/// Present In-App Notification from remote notification (if present).
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
  FBNotificationsManager *notificationsManager = [FBNotificationsManager sharedManager];
  [notificationsManager presentPushCardForRemoteNotificationPayload:userInfo
                                                 fromViewController:nil
                                                         completion:^(FBNCardViewController * _Nullable viewController, NSError * _Nullable error) {
                                                           if (error) {
                                                             completionHandler(UIBackgroundFetchResultFailed);
                                                           } else {
                                                             completionHandler(UIBackgroundFetchResultNewData);
                                                           }
                                                         }];
}

Swift

/// Import the following in the same source file
import FBNotifications

/// Add the following functions to the implementation of application delegate

/// This function is optional if you already have push notifications set up.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  let settings = UIUserNotificationSettings(forTypes: [.Alert], categories: nil)
  UIApplication.sharedApplication().registerUserNotificationSettings(settings)
  UIApplication.sharedApplication().registerForRemoteNotifications
  return true
}


/// Present In-App Notification from remote notification (if present).
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
  FBNotificationsManager.sharedManager().presentPushCardForRemoteNotificationPayload(userInfo, fromViewController: nil) { viewController, error in
    if let _ error = error {
      completionHandler(.Failed)
    } else {
      completionHandler(.NewData)
    }
  }
}