当应用程序完全关闭(不在后台)时如何响应本地通知?
当应用程序在后台或前台运行时,一切正常。但是当应用程序关闭并且我试图回复通知时,仅"application didFinishLaunchingWithOptions"
被呼叫,"userNotificationCenter didRecive response
” 没有回答。
我发现这个问题(应用程序关闭时如何处理 UNNotificationAction? https://stackoverflow.com/questions/39655180/how-to-handle-unnotificationaction-when-app-is-closed)但就我而言,它既不能在真实设备上也不能在模拟器中工作。
我还注意到该功能"UNUserNotificationCenter.current().getDeliveredNotifications()"
回报nil
当我在应用程序关闭时响应通知时。
For iOS 9 及更低版本 in application(_:didFinishLaunchingWithOptions:)
你会检查是否launchOptions
包含localNotification: UIApplicationLaunchOptionsKey
。如果是这样,您就会知道该应用程序刚刚由于本地通知而刚刚启动(即用户单击了通知)。
For iOS 10+ you must注册你的UNUserNotificationCenterDelegate
in application(_:willFinishLaunchingWithOptions:)
or application(_:didFinishLaunchingWithOptions:)
.
您的代表的职能:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
如果您的应用程序刚刚启动或只是坐在后台,则会被调用。
这一新 API 的优点是只有一个地方可以处理本地通知操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)