iOS10应用关闭时如何响应本地通知?

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

应用完全关闭(非后台)时如何响应本地通知?

当应用 运行 在后台或前台运行时一切正常。但是当应用程序关闭并且我试图回复通知时,只有 "application didFinishLaunchingWithOptions" 被调用,"userNotificationCenter didRecive response" 没有回答。

我发现了这个问题 (How to handle UNNotificationAction when app is closed?),但就我而言,它在真实设备和模拟器中都不起作用。

我还注意到 "UNUserNotificationCenter.current().getDeliveredNotifications()" returns nil 当我在应用程序关闭时响应通知时

对于 iOS 9 和 application(_:didFinishLaunchingWithOptions:) 中的 ,您将检查 launchOptions 是否包含 localNotification: UIApplicationLaunchOptionsKey。如果是这样,您就会知道该应用程序由于本地通知(即用户单击通知)而刚刚启动。

对于iOS 10+,您必须application(_:willFinishLaunchingWithOptions:)或[中注册您的UNUserNotificationCenterDelegate =11=]。

您的代表职能:

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

如果您的应用刚刚启动或只是在后台运行,将被调用。 这个新的 API 的优点是只有一个地方可以处理本地通知操作。