Leanplum's user guides and developer documentation.

Custom push notification swizzling for iOS

4 months ago by Mayank Sanganeria

By default, Leanplum's iOS SDK uses method swizzling to collect push tokens. If your iOS app has multiple different SDKs installed that attempt to swizzle the push methods, it can cause problems where some push notification methods are not called.

To avoid issues collecting push tokens, you can disable automatic method swizzling for push notification methods and call the Leanplum push notification methods manually.

Set up push token collection manually

Here are the steps to call the push methods without swizzling:

  1. Disable swizzling by putting a boolean flag for LeanplumSwizzlingEnabled in your app Info.plist to NO.
  2. Make sure the following methods are present in your AppDelegate.m file. Make sure to call the corresponding method on the Leanplum object.
// At minimum:
+ (void)application:(UIApplication *) didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token;
+ (void)application:(UIApplication *) didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
+ (void)application:(UIApplication *) didReceiveRemoteNotification:(NSDictionary *)userInfo;

// Optional:
+ (void)application:(UIApplication *) didRegisterUserNotificationSettings:(UIUserNotificationSettings*) notificationSettings;
+ (void)application:(UIApplication *) didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0));
+ (void)application:(UIApplication *) didReceiveLocalNotification:(UILocalNotification *)localNotification;
// At minimum:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any])


// Optional:
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings)
func application(_ application: UIApplication, 
didReceiveRemoteNotification userInfo: [AnyHashable : Any])
func application(_ application: UIApplication, 
               didReceive notification: UILocalNotification)

Here’s a sample implementation:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    [Leanplum didReceiveRemoteNotification:userInfo 

fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    [Leanplum didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    [Leanplum didFailToRegisterForRemoteNotificationsWithError:error];
}
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    Leanplum.didReceiveRemoteNotification(userInfo)
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    Leanplum.didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    Leanplum.didFailToRegisterForRemoteNotificationsWithError(error)
}