{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","settings":"","params":[],"results":{"codes":[]}},"next":{"description":"","pages":[]},"title":"iOS: Notification Handling","type":"basic","slug":"ios-notification-handling","excerpt":"","body":"## Manual Integraton\n\n### Token Handling\nSome third party libraries and frameworks can interfere with the Sailthru Mobile SDK's ability to collect push tokens and to handle incoming notifications. If devices are registering with Sailthru Mobile, but without push tokens, you may need to manually forward notifications and device tokens to the Sailthru Mobile iOS SDK.\n\nTo do this, you will need to override a few delegate methods on your application delegate.\n\nIn `application:didRegisterForRemoteNotificationsWithDeviceToken:` call the `setDeviceTokenInBackground` method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {\\n  // Forward the device token data to the Sailthru Mobile iOS SDK\\n  [[SailthruMobile new] setDeviceTokenInBackground:deviceToken];\\n}\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \" func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {\\n  // Forward the device token data to the Sailthru Mobile iOS SDK\\n  SailthruMobile().setDeviceTokenInBackground(deviceToken)\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n### Notification Handling\n\nTo forward notifications to the Sailthru Mobile iOS SDK call the `handleNotification:` method on Sailthru Mobile with the notification received from `application:didReceiveRemoteNotification:` and `applicationDidReceiveRemoteNotification:fetchCompletionHandler:` where appropriate:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo\\n  {\\n\\t  // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\\n\\t  [[SailthruMobile new] handleNotificationPayload:userInfo];\\n  }\\n\\n- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {\\n  // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\\n  [[SailthruMobile new] handleNotificationPayload:userInfo];\\n}\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {\\n    // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\\n    SailthruMobile().handleNotificationPayload(userInfo)\\n}\\n    \\nfunc application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {\\n    // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\\n    SailthruMobile().handleNotificationPayload(userInfo)\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nAs of iOS 10, notifications are delivered through to the `UNUserNotificationCenterDelegate` instead of the `AppDelegate`. In order to provide notifications to the Sailthru Mobile SDK you can conform to a `UNUserNotificationCenterDelegate` object and implement the `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:`. See an example below.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {\\n  \\t// Forward the notification response to the Sailthru Mobile iOS SDK\\n  \\t[[SailthruMobile new] handleNotificationResponse:response];\\n    completionHandler();\\n}\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: :::at:::escaping () -> Void) {\\n\\t\\t// Forward the notification response to the Sailthru Mobile iOS SDK\\n\\t\\tSailthruMobile().handle(response)\\n\\t\\tcompletionHandler()\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n## Foreground Presentation\nAs of iOS 10, notifications may appear inside the app while the app is open. By default, the Sailthru Mobile 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {\\n    // Forward the notification to the Sailthru Mobile iOS SDK\\n  \\t[[SailthruMobile new] handlePresentNotification:notification];\\n    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);\\n}\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {\\n\\t  // Forward the notification to the Sailthru Mobile iOS SDK\\n  \\tSailthruMobile().handlePresent(notification)\\n    completionHandler([.alert, .badge, .sound])\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]","updates":[],"order":0,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5e6561589eb1dc0060defd3e","createdAt":"2020-03-08T21:19:20.573Z","user":"5b0b7a46a26e6400036604fd","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"iOS Techniques","slug":"ios-techniques","order":9999,"from_sync":false,"reference":false,"_id":"5e6986ca2c6652006791b6e8","createdAt":"2020-03-12T00:48:10.039Z","version":"5e6156bf5e4a51006dcd818c","project":"55e67aaa9cc7c62b00c4a1ea","__v":0},"version":{"version":"1.5","version_clean":"1.5.0","codename":"ST Rebrand","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["5e6156bf5e4a51006dcd8120","5e6156bf5e4a51006dcd8121","5e6156bf5e4a51006dcd8122","5e6156bf5e4a51006dcd8123","5e6156bf5e4a51006dcd8124","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","5e6156bf5e4a51006dcd8125","5e6156bf5e4a51006dcd8126","5af0fe494ca2730003cbc98a","5af0fe55ec80af0003804ca2","5e69868cbd5dcb006b35867b","5e6986ca2c6652006791b6e8"],"_id":"5e6156bf5e4a51006dcd818c","project":"55e67aaa9cc7c62b00c4a1ea","__v":2,"forked_from":"5b720760c44b7600034b7a08","createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z"},"project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"parentDoc":null}

iOS: Notification Handling


## Manual Integraton ### Token Handling Some third party libraries and frameworks can interfere with the Sailthru Mobile SDK's ability to collect push tokens and to handle incoming notifications. If devices are registering with Sailthru Mobile, but without push tokens, you may need to manually forward notifications and device tokens to the Sailthru Mobile 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: [block:code] { "codes": [ { "code": "- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {\n // Forward the device token data to the Sailthru Mobile iOS SDK\n [[SailthruMobile new] setDeviceTokenInBackground:deviceToken];\n}", "language": "objectivec" }, { "code": " func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {\n // Forward the device token data to the Sailthru Mobile iOS SDK\n SailthruMobile().setDeviceTokenInBackground(deviceToken)\n}", "language": "swift" } ] } [/block] ### Notification Handling To forward notifications to the Sailthru Mobile iOS SDK call the `handleNotification:` method on Sailthru Mobile with the notification received from `application:didReceiveRemoteNotification:` and `applicationDidReceiveRemoteNotification:fetchCompletionHandler:` where appropriate: [block:code] { "codes": [ { "code": "- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo\n {\n\t // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\n\t [[SailthruMobile new] handleNotificationPayload:userInfo];\n }\n\n- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {\n // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\n [[SailthruMobile new] handleNotificationPayload:userInfo];\n}", "language": "objectivec" }, { "code": "func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {\n // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\n SailthruMobile().handleNotificationPayload(userInfo)\n}\n \nfunc application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {\n // Forward the notification's userInfo to the Sailthru Mobile iOS SDK\n SailthruMobile().handleNotificationPayload(userInfo)\n}", "language": "swift" } ] } [/block] As of iOS 10, notifications are delivered through to the `UNUserNotificationCenterDelegate` instead of the `AppDelegate`. In order to provide notifications to the Sailthru Mobile SDK you can conform to a `UNUserNotificationCenterDelegate` object and implement the `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:`. See an example below. [block:code] { "codes": [ { "code": "- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {\n \t// Forward the notification response to the Sailthru Mobile iOS SDK\n \t[[SailthruMobile new] handleNotificationResponse:response];\n completionHandler();\n}", "language": "objectivec" }, { "code": "func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {\n\t\t// Forward the notification response to the Sailthru Mobile iOS SDK\n\t\tSailthruMobile().handle(response)\n\t\tcompletionHandler()\n}", "language": "swift" } ] } [/block] ## Foreground Presentation As of iOS 10, notifications may appear inside the app while the app is open. By default, the Sailthru Mobile 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. [block:code] { "codes": [ { "code": "- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {\n // Forward the notification to the Sailthru Mobile iOS SDK\n \t[[SailthruMobile new] handlePresentNotification:notification];\n completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);\n}", "language": "objectivec" }, { "code": "func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {\n\t // Forward the notification to the Sailthru Mobile iOS SDK\n \tSailthruMobile().handlePresent(notification)\n completionHandler([.alert, .badge, .sound])\n}", "language": "swift" } ] } [/block]