How to respond to local notification when app is closed in iOS10?

…衆ロ難τιáo~ 提交于 2019-12-04 14:31:44

For iOS 9 and lower in application(_:didFinishLaunchingWithOptions:) you would check if launchOptions contains localNotification: UIApplicationLaunchOptionsKey. If so you would know that the app just freshly started because of local notification (i.e. user clicked on notification).

For iOS 10+ you must register your UNUserNotificationCenterDelegate in application(_:willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions:).

Your delegate's function:

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

will be called if your app freshly started or it was just sitting in the background. The advantage of this new API is that there is only one place to handle local notification actions.

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