Auto Integration

SDK auto integration details

By default the iOS SDK will integrate with the push notification handling in your application, allowing it to process notification data and settings without any manual integration steps on your part. However, there are some libraries that can conflict with this auto integration and some situations where you may desire more manual control over this handling. In these cases, you can turn off auto integration using the following method.

Objective-C

[[SailthruMobile new] setAutoIntegrationEnabled:NO];

Swift

SailthruMobile().setAutoIntegrationEnabled(false)
Note: You must disable auto integration before calling the startEngine method. Once auto integration has been turned off, there are some extra methods you will need to implement in your application in order for the SDK to receive push notification related data.

UNUserNotificationCenterDelegate methods

These methods are called in your UNUserNotificationCenterDelegate implementation. You need to set this in the UNUserNotificationCenter in order to handle push notifications in your app from iOS 10+.

Objective-C

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    // Pass the notification to the SDK
    [[SailthruMobile new] handlePresentNotification:notification];
  
    // Call the completion handler
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}

Swift

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // Pass the notification to the SDK
    SailthruMobile().handlePresent(notification)
  
    // Call the completion handler
    completionHandler([.badge, .sound])
}
The userNotificationCenter:willPresentNotification:withCompletionHandler: method is called when a notification is received when the application is running in the foreground.

Objective-C

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    // Pass the notification response to the SDK.
    [[SailthruMobile new] handleNotificationResponse:response];

    // Call the completion handler
    completionHandler();
}

Swift

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // Pass the notification response to the SDK.
    SailthruMobile().handle(response)
  
    // Call the completion handler
    completionHandler()
}
The userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: method is called when a notification has been interacted with by the user, which can be:
  • Tapping the notification
  • Tapping a notification action
  • Dismissing the notification, if custom dismissal handling has been requested for that category

UNUserNotificationCenter methods

After requesting push notification authorization from the UNUserNotificationCenter you can update the platform with the authorized notification options using the following method.

Objective-C

[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
    // Send push authorization settings to platform
    [[SailthruMobile new] syncNotificationSettings];
}];

Swift

UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
    // Send push authorization settings to platform
    SailthruMobile().syncNotificationSettings()
}

UIApplicationDelegate methods

These methods are called in your application's UIApplicationDelegate implementation and include token handling and push payload handling.

Objective-C

- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [[SailthruMobile new] setDeviceTokenInBackground:deviceToken];
}

Swift

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    SailthruMobile().setDeviceTokenInBackground(deviceToken)
}
The application:didRegisterForRemoteNotificationsWithDeviceToken: method is called when an app has registered for remote notifications using the [[UIApplication sharedApplication] registerForRemoteNotifications] method. It will be called to return the APNS token to the application, which must then be supplied to in the SDK in order for the platform to send push notifications to the device.

Objective-C

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // Pass the notification payload to the SDK.
    [[SailthruMobile new] handleNotificationPayload:userInfo];
  
    // Call the completion handler
    completionHandler(UIBackgroundFetchResultNewData);
}

Swift

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // Pass the notification payload to the SDK.
    SailthruMobile().handleNotificationPayload(userInfo)
  
    // Call the completion handler
    completionHandler(.newData)
}
The application:didReceiveRemoteNotification:fetchCompletionHandler: method serves different functions on different versions of iOS. For iOS 8 & 9 it is the sole source of data for received notifications. For iOS 10+ it is called at the time the notification is received (if possible). If your app has requested permission to launch in the background for remote notifications and the app is not running, it will launch the app into the background if it can to handle this method. However, it will not launch if the user has manually cleared the app from memory.

Contact us

Top