LogoLogo
  • Home
  • Integration & SDKs
    • Web / Browser Push
      • Wordpress Integration Steps
      • Wix Integration Steps
      • Safari
        • Safari on Desktop
        • Safari on Mobile (iOS / iPadOS)
      • SDK
        • SDK Methods
        • SDK Events
        • Customizing Prompt CSS
        • AMP Support
        • E-Commerce / Abandoned Cart
    • Native App Push
      • Apple / iOS
        • P8 Key or P12 Cert Setup
        • SDK: Swift / Obj-C
          • Activity Tracking
          • Deep Linking
          • Live Activities
          • App Messages
          • Identity Synchronization
          • SDK Methods
          • SDK Delegates
        • SDK: React Native
        • SDK: Flutter
        • Advanced
          • Self-Managed Integration
        • iOS SDK Changelog
      • Android
        • Firebase App Setup
        • SDK: Kotlin / Java
          • Deep Linking
          • Activity Tracking
          • App Messages
          • Identity Synchronization
          • SDK Methods
          • SDK Callbacks
          • Live Activities
        • SDK: React Native
        • SDK: Flutter
        • Advanced
          • Self-Managed Integration
        • Android SDK Changelog
      • React Native
        • Android: Firebase App Setup
        • iOS: P8 Key or P12 Cert Setup
        • SDK: React Native
          • Deep Linking
          • Activity Tracking
          • Subscriber Matching
          • Live Activities
          • App Messages
          • SDK Methods
      • Flutter
        • Android: Firebase App Setup
        • iOS: P8 Key or P12 Cert Setup
        • SDK: Flutter
          • Deep Linking
          • Activity Tracking
          • Subscriber Matching
          • Live Activities
          • App Messages
          • SDK Methods
  • API
    • API Access Management
    • API Reference
  • Platform
    • Dashboard
    • Notifications
      • A/B Testing
      • Custom Buttons
      • Notification Previews
      • Inline Segmentation
      • Notification Templates
      • Macros
      • Native App Push Notifications
    • App Messages
    • Segments
      • Custom Geo Segmentation
    • Campaigns
    • Insights
    • User Management
    • Organizations
      • Multi-Factor Authentication
      • Single Sign On
        • Google Workspace
        • Microsoft Azure AD
        • Okta Workforce
      • Multi-Domain Notifications
      • Multi-Domain Segments
      • API Access
    • Multi-Channel Notifications
  • Info Center
    • Launch Guide & Best Practices
      • Landing Domain Whitelist
    • Web / Browser Push
      • Common Questions
      • Browser Support
      • Retrieve Push User ID
      • Not Getting Prompted
      • Not Receiving Notifications
      • How to Unsubscribe
    • Native App Push
      • Retrieve Push User ID
    • Workflow Planning
Powered by GitBook
On this page
  • UIApplicationDelegate Example
  • UNUserNotificationCenterDelegate Example
  1. Integration & SDKs
  2. Native App Push
  3. Apple / iOS
  4. Advanced

Self-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 self-managed integration if swizzling is incompatible with existing code. If you choose to use a self-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()
[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)
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [PushSDK disableMethodSwizzling];

    [PushSDK setConfigurationAppKey:@"REPLACE_WITH_SDK_KEY" withLaunchOptions:launchOptions];

    [PushSDK showNativeNotificationPermissionPrompt:^(BOOL granted, UNNotificationSettings * _Nonnull settings, NSError * _Nullable error) {
        NSLog(@"User accepted permissions: %@", granted ? @"YES" : @"NO");
    }];

    return YES;
}

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

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    [PushSDK application:application didFailToRegisterForRemoteNotificationsWithError: error];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [PushSDK application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {
        // Capture the PushSDK UIBackgroundFetchResult, modify as needed for your own logic
        completionHandler(result);
    }];
}

// For integrations targeting iOS version 10 or earlier
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [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()
    }
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    [PushSDK userNotificationCenter:center willPresent:notification withCompletionHandler:^(UNNotificationPresentationOptions options) {
        // Capture the PushSDK UNNotificationPresentationOptions, modify as needed for your own logic
        completionHandler(options);
    }];    
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
    [PushSDK userNotificationCenter:center didReceive:response withCompletionHandler:^{
        completionHandler();
    }];
}
PreviousAdvancedNextiOS SDK Changelog

Last updated 5 months ago