应用关闭时如何处理UNNotificationAction?

编程入门 行业动态 更新时间:2024-10-17 21:29:10
本文介绍了应用关闭时如何处理UNNotificationAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在应用关闭(不在后台)时如何处理新的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?

本文发布于:2023-11-26 23:00:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   UNNotificationAction

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!