在应用关闭(不在后台)时如何处理新的iOS10通知操作?
How to handle new iOS10 Notification Action when app is closed (not in background) ?
当应用程序最小化时,一切都可以正常使用:
when app is minimalized everything works fine with:
UNUserNotificationCenter.current().delegate = x并在其中处理
class x: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) { } }但是当应用程序关闭并且用户在通知中点击操作时什么也没叫...也许我无法处理后台任务,并且我总是必须启动应用程序?
but nothing is called when app is closed and user tap action in notification... maybe i can't handle background task and i always have to launch app?
推荐答案通知操作按钮的处理可以在扩展和 Containing App 中进行.
The notification action buttons handling can be done in both Extension as well as in Containing App.
点击操作按钮后,如果需要,手柄首先进入 Extension ,然后进入 Containing App .如果 Extension 不处理通知操作,则该句柄将传递到包含的应用程序.
When the action button is tapped, the handle first goes to the Extension and then to the Containing App if required. If Extension does not handle the notification action, the handle is passed on to the containing app.
轻按一个按钮即可启动您的应用(在前台或背景),并有机会执行指示的操作.
Tapping a button launches your app (either in the foreground or background) and gives you a chance to perform the indicated action.
处理扩展程序:
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) { //You need to handle all the actions that appear with notification.. completion(.dismissAndForwardAction) }完成关闭的值类型为UNNotificationContentExtensionResponseOption:
The completion closure takes a value of type UNNotificationContentExtensionResponseOption:
enum UNNotificationContentExtensionResponseOption : UInt { case doNotDismiss //the custom UI is not dismissed after handling the action case dismiss //the custom UI is dismissed after handling the action case dismissAndForwardAction //the custom UI is dismissed after handling the action and the control is then passed to containing app for any additional handling }包含应用的处理
extension AppDelegate : UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { // Action handling - handling for all actions is not required completionHandler() } }有关更多信息,您可以参考此内容( github/pgpt10/RichNotificationSample )教程.
For more you can refer to this(github/pgpt10/RichNotificationSample) tutorial.
更多推荐
应用关闭时如何处理UNNotificationAction?
发布评论