UNUserNotificationCenter didRecieve Response is not called when the application is terminated

UNUserNotificationCenter function is not called when the button is pressed Action Chat in the notice after the 3D-touch, if the application is inactive (not even in the background, or has not been completed). I used the "attach to process by name" in Xcode to debug the application, when an application has been terminated. Here is the code:

import UIKit import Mixpanel import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //setup mixpanel self.handlePushNotificationWithUserInfo(launchOptions: launchOptions) //ask for push notification perms return true } 

When a notification pop-up message (sent from MixPanel), this function is called first,

Call 1:

  func handlePushNotificationWithUserInfo(launchOptions: [NSObject: AnyObject]?) { //Handle PushNotification when app is opened } 

Then he goes here

Call 2:

  //register for notification func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } center.delegate = self let actionChat = UNNotificationAction(identifier: Constants.ActionType.CHAT.rawValue, title: "Chat", options: [.foreground]) let categoryOptions = UNNotificationCategoryOptions(rawValue: 0) let customerSupportCategory = UNNotificationCategory(identifier: Constants.NotificationType.CUSTOMER_SUPPORT.rawValue, actions: [actionChat], intentIdentifiers: [], options: categoryOptions) center.setNotificationCategories([customerSupportCategory]) } application.registerForRemoteNotifications() } didRegister notificationSettings: UIUserNotificationSettings) {  //register for notification func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } center.delegate = self let actionChat = UNNotificationAction(identifier: Constants.ActionType.CHAT.rawValue, title: "Chat", options: [.foreground]) let categoryOptions = UNNotificationCategoryOptions(rawValue: 0) let customerSupportCategory = UNNotificationCategory(identifier: Constants.NotificationType.CUSTOMER_SUPPORT.rawValue, actions: [actionChat], intentIdentifiers: [], options: categoryOptions) center.setNotificationCategories([customerSupportCategory]) } application.registerForRemoteNotifications() } ]) {(granted, error) in  //register for notification func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } center.delegate = self let actionChat = UNNotificationAction(identifier: Constants.ActionType.CHAT.rawValue, title: "Chat", options: [.foreground]) let categoryOptions = UNNotificationCategoryOptions(rawValue: 0) let customerSupportCategory = UNNotificationCategory(identifier: Constants.NotificationType.CUSTOMER_SUPPORT.rawValue, actions: [actionChat], intentIdentifiers: [], options: categoryOptions) center.setNotificationCategories([customerSupportCategory]) } application.registerForRemoteNotifications() } , title: "Chat", options: [.foreground])  //register for notification func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } center.delegate = self let actionChat = UNNotificationAction(identifier: Constants.ActionType.CHAT.rawValue, title: "Chat", options: [.foreground]) let categoryOptions = UNNotificationCategoryOptions(rawValue: 0) let customerSupportCategory = UNNotificationCategory(identifier: Constants.NotificationType.CUSTOMER_SUPPORT.rawValue, actions: [actionChat], intentIdentifiers: [], options: categoryOptions) center.setNotificationCategories([customerSupportCategory]) } application.registerForRemoteNotifications() } 

Call 3:

  // remote notification func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { ....Some Code.... } 

But below the function is not called. But if the application is running in the background, the following function is called and is operating normally. OTHERWISE App comes to the fore and not after that chat.

  // action buttons in enhanced Notification @available(iOS 10, *) func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { guard let action = Constants.ActionType(rawValue: response.actionIdentifier) else { completionHandler() return } switch action { case .CHAT: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) default: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) } completionHandler() } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound]) } } response.actionIdentifier) else {  // action buttons in enhanced Notification @available(iOS 10, *) func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { guard let action = Constants.ActionType(rawValue: response.actionIdentifier) else { completionHandler() return } switch action { case .CHAT: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) default: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) } completionHandler() } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound]) } } "chat") as URL?)  // action buttons in enhanced Notification @available(iOS 10, *) func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { guard let action = Constants.ActionType(rawValue: response.actionIdentifier) else { completionHandler() return } switch action { case .CHAT: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) default: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) } completionHandler() } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound]) } } )  // action buttons in enhanced Notification @available(iOS 10, *) func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { guard let action = Constants.ActionType(rawValue: response.actionIdentifier) else { completionHandler() return } switch action { case .CHAT: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) default: _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) } completionHandler() } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound]) } } 

This function is never called because it depreciated in iOS 10 through userNotificationCenter (), but not sure. Please explain this as well.

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { ....Some Code.... } 

I use the iPhone 6s iOS 10 as a debugging device. 8 XCode beta-3

+5
source share
3 answers

From my own experiments receive local notifications Swift Xcode 3 and 8 is as follows:

conformity

  • Meets UNUserNotificationCenterDelegate

     class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { .... } 
  • Register as a delegate to the Notification Center:

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let center = UNUserNotificationCenter.current() center.delegate = self return true } didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let center = UNUserNotificationCenter.current() center.delegate = self return true } 

delegates methods

  • Respond to missed notifications (for example, an application for viewing users when sending a notification)

     func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { print(notification.request.content.userInfo) } willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { print(notification.request.content.userInfo) } 
  • Respond to notifications (e.g. user-open notifications)

     func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { print(response.notification.request.content.userInfo) } didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { print(response.notification.request.content.userInfo) } 
+6
source

Swift 3.1 Update

  • Conforms to UNUserNotificationCenterDelegate

  • Sign up as a delegate of the Center for notifications:

 UNUserNotificationCenter.current().delegate = self 
  • Delegates methods.
 public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(response.notification.request.content.categoryIdentifier) } public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print(notification.request.content.categoryIdentifier) } , didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(response.notification.request.content.categoryIdentifier) } public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print(notification.request.content.categoryIdentifier) } , willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(response.notification.request.content.categoryIdentifier) } public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print(notification.request.content.categoryIdentifier) } 
+1
source

When the application does not start or is not killed by the user, and the notification is received, then in such a scenario, you need to handle in didFinishLaunchingWithOptions and check whether the application is notified through an open and act accordingly.

// Check if the notification is started

 if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] { notificationReceived(notification) } ] as? if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] { notificationReceived(notification) } 
0
source

All Articles