Managed Integration

To make integrations easier the PushSDK automatically integrates itself into your application delegate and user notification center by using method swizzling. You may want to disable the automatic integration in several scenarios including:

  • Conflicts with other SDKs that also include swizzling or other automatic integrations

  • Conflicting third party development solutions

  • Our method swizzling conflicts with your existing architecture

We suggest that you only consider a managed integration if swizzling is incompatible with existing code. If you choose to use a managed integration you will need to ensure that any new releases to the PushSDK do not add additional methods that must be called manually.

Add the following method call before calling PushSDK.setConfiguration:

PushSDK.disableMethodSwizzling()

This must be invoked before calling PushSDK.setConfiguration(appKey: myAppKey, withLaunchOptions: launchOptions)

Once swizzling has been disabled you'll also be required to place calls to the PushSDK to invoke the methods that are no longer swizzled. If you do not implement all of the following methods the PushSDK may not function properly.

The methods you must call manually are:

  • application:didRegisterForRemoteNotificationsWithDeviceToken:

  • application:didFailToRegisterForRemoteNotificationsWithError:

  • application:didReceiveRemoteNotification:fetchCompletionHandler:

  • userNotificationCenter:willPresent:withCompletionHandler:

  • userNotificationCenter:didReceive:withCompletionHandler:

  • If you are targeting iOS version 10 or earlier:

    • application:didReceiveRemoteNotification:

UIApplicationDelegate Example

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    PushSDK.disableMethodSwizzling()

    PushSDK.setConfiguration(appKey: "REPLACE_WITH_SDK_KEY", withLaunchOptions: launchOptions)

    PushSDK.showNativeNotificationPermissionPrompt() { granted, settings, error in
        // optional callback
        print("User accepted permissions: \(granted)")
    }

    return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    PushSDK.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    PushSDK.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    PushSDK.application(application, didReceiveRemoteNotification: userInfo) { result in
        // Capture the PushSDK UIBackgroundFetchResult, modify as needed for your own logic
        completionHandler(result)
    }
}

// For integrations targeting iOS version 10 or earlier
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    PushSDK.application(application, didReceiveRemoteNotification: userInfo)
}

UNUserNotificationCenterDelegate Example

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    PushSDK.userNotificationCenter(center, willPresent: notification) { options in
        // Capture the PushSDK UNNotificationPresentationOptions, modify as needed for your own logic
        completionHandler(options)
    }

}

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    PushSDK.userNotificationCenter(center, didReceive: response) {
        completionHandler()
    }
}

Last updated