How to determine whether the app is opened from NotificationCenter (Local Notification) or the app icon when app is killed

本秂侑毒 提交于 2020-03-05 09:34:06

问题


My App receives a local notification Not a remote Notification. How can I know if the app is opened by pressing the notification and not the app icon when the app is killed( Not running in background or foreground). Previosuly, I used the following method

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if ((launchOptions?[UIApplication.LaunchOptionsKey.loaclNotification] != nil))
    {
        print("here")
    }
 }

however this function is deprecated as of iOS 10

[UIApplication.LaunchOptionsKey.loaclNotification] != nil)

and apple documentation suggests to use the following method.

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

}

This method is called only when the app is in the foreground or background. Any help will be appreciated


回答1:


How can I know if the app is opened by pressing the notification and not the app icon

As you said, implement the UNUserNotificationCenterDelegate method didReceive. If the user tapped your notification, it will be called, launching your app if it isn’t running.



来源:https://stackoverflow.com/questions/54581191/how-to-determine-whether-the-app-is-opened-from-notificationcenter-local-notifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!