iOS: Notification Handling

Manual Integraton

Token Handling

Some third party libraries and frameworks can interfere with the Sailthru Mobile SDK's ability to collect push tokens and to handle incoming notifications. If devices are registering with Sailthru Mobile, but without push tokens, you may need to manually forward notifications and device tokens to the Sailthru Mobile iOS SDK.

To do this, you will need to override a few delegate methods on your application delegate.

In application:didRegisterForRemoteNotificationsWithDeviceToken: call the setDeviceTokenInBackground method:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Forward the device token data to the Sailthru Mobile iOS SDK
  [[SailthruMobile new] setDeviceTokenInBackground:deviceToken];
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
  // Forward the device token data to the Sailthru Mobile iOS SDK
  SailthruMobile().setDeviceTokenInBackground(deviceToken)
}

Notification Handling

To forward notifications to the Sailthru Mobile iOS SDK call the handleNotification: method on Sailthru Mobile with the notification received from application:didReceiveRemoteNotification: and applicationDidReceiveRemoteNotification:fetchCompletionHandler: where appropriate:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  {
      // Forward the notification's userInfo to the Sailthru Mobile iOS SDK
      [[SailthruMobile new] handleNotificationPayload:userInfo];
  }

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
  // Forward the notification's userInfo to the Sailthru Mobile iOS SDK
  [[SailthruMobile new] handleNotificationPayload:userInfo];
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    // Forward the notification's userInfo to the Sailthru Mobile iOS SDK
    SailthruMobile().handleNotificationPayload(userInfo)
}
    
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // Forward the notification's userInfo to the Sailthru Mobile iOS SDK
    SailthruMobile().handleNotificationPayload(userInfo)
}

As of iOS 10, notifications are delivered through to the UNUserNotificationCenterDelegate instead of the AppDelegate. In order to provide notifications to the Sailthru Mobile SDK you can conform to a UNUserNotificationCenterDelegate object and implement the userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:. See an example below.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    // Forward the notification response to the Sailthru Mobile iOS SDK
    [[SailthruMobile new] handleNotificationResponse:response];
    completionHandler();
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // Forward the notification response to the Sailthru Mobile iOS SDK
        SailthruMobile().handle(response)
        completionHandler()
}

Foreground Presentation

As of iOS 10, notifications may appear inside the app while the app is open. By default, the Sailthru Mobile SDK hides these, and when In App Messages are associated, a roll-down In App Notification for those are shown. You can override this behaviour by conforming an object UNUserNotificationCenterDelegate and implementing userNotificationCenter:willPresentNotification:withCompletionHandler:. See an example below.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    // Forward the notification to the Sailthru Mobile iOS SDK
    [[SailthruMobile new] handlePresentNotification:notification];
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      // Forward the notification to the Sailthru Mobile iOS SDK
    SailthruMobile().handlePresent(notification)
    completionHandler([.alert, .badge, .sound])
}

Did this page help you?