gpt4 book ai didi

ios - 应用程序关闭时如何处理 UNNotificationAction?

转载 作者:行者123 更新时间:2023-12-04 19:36:54 24 4
gpt4 key购买 nike

当应用程序关闭(不在后台)时如何处理新的 iOS10 通知操作?

当应用程序被最小化时,一切正常:

UNUserNotificationCenter.current().delegate = x

并处理它
class x: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
}
}

但是当应用程序关闭并且用户在通知中点击操作时什么都不会调用......也许我无法处理后台任务并且我总是必须启动应用程序?

最佳答案

通知操作按钮处理可以在 Extension 中完成以及在 Containing App .

当点击操作按钮时, handle 首先转到 Extension然后到 Containing App如果需要的话。如 Extension不处理通知 Action ,句柄传递给 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 类型的值:
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()
}
}

更多你可以引用这个( https://github.com/pgpt10/RichNotificationSample)教程。

关于ios - 应用程序关闭时如何处理 UNNotificationAction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com