Guides
Guides

iOS: Notification Handling

Manual Integraton

Token Handling

Some third party libraries and frameworks can interfere with the Marigold SDK's ability to collect push tokens and to handle incoming notifications. If devices are registering with Marigold, but without push tokens, you may need to manually forward notifications and device tokens to the Marigold 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 Marigold iOS SDK
    [[Marigold new] setDeviceTokenInBackground:deviceToken];
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    // Forward the device token data to the Marigold iOS SDK
    Marigold().setDeviceTokenInBackground(deviceToken)
}

Notification Handling

Notifications are delivered through to the UNUserNotificationCenterDelegate. In order to provide notifications to the Marigold 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 Marigold iOS SDK
    [[Marigold new] handleNotificationResponse:response];
    completionHandler();
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // Forward the notification response to the Marigold iOS SDK
    Marigold().handle(response)
    completionHandler()
}

Foreground Presentation

Notifications may appear inside the app while the app is open. By default, the Marigold 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 Marigold iOS SDK
    [[Marigold new] handlePresentNotification:notification];
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // Forward the notification to the Marigold iOS SDK
    Marigold().handlePresent(notification)
    completionHandler([.alert, .badge, .sound])
}