How can I get notified and access the notification payload if a user clicks on a local notification while the app is in the suspended state and launches the application? Whenever my iOS app is opened from the background state the UNNotification response function is called. The local notification launched by key has been deprecated and I can't figure out how to determine if the app has been launched by a local notification. I need to use the notification payload to populate the view controller properly.
I am using this function to get the notification response while the app is in the background state:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive
response: UNNotificationResponse, withCompletionHandler completionHandler:
@escaping () -> Void) {
setup view here using payload
}
The problem is likely that you are not configuring the UNUserNotificationCenterDelegate early enough. You need to do that very early, as in didFinishLaunching
, so that the runtime knows where to send didReceive
. This is how I do it:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self.notifHelper
return true
}