应用关闭时如何处理UNNotificationAction? [英] How to handle UNNotificationAction when app is closed?

本文介绍了应用关闭时如何处理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()
    }
}

有关更多信息,您可以参考此内容( https://github.com/pgpt10/RichNotificationSample )教程.

For more you can refer to this(https://github.com/pgpt10/RichNotificationSample) tutorial.

这篇关于应用关闭时如何处理UNNotificationAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆