OGeek|极客世界-中国程序员成长平台

标题: ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:44
标题: ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption

我正在设置多个本地通知,并且我已将操作数设置为通知。像打盹、显示、关闭。

    func didReceive(_ response: UNNotificationResponse, completionHandler completion:  @escaping (UNNotificationContentExtensionResponseOption) -> Void) {


    let id = response.notification.request.identifier

       if response.actionIdentifier == "SnoozeId" {
        completion(UNNotificationContentExtensionResponseOption.dismiss)

           }}

在 didReceive - 如果操作是贪睡 - 执行贪睡,然后我关闭通知。当我解雇所有其他通知时,通知中心都消失了。如果我有两个 Notification A 和 B 。 如果我长按并在 A 上执行贪睡。 A 和 B 都从通知中心消失了。它应该只解雇 A。



Best Answer-推荐答案


以下是使用我的应用进行测试的结果。

为了更好地回答您的问题,请提供更多信息,例如 - 通知的 json 及其 userInfo(也许您正在对通知进行分组并一次全部关闭?)以及您调用的函数会导致调用didReceive(...).

如果你打电话

self.extensionContext?.dismissNotificationContentExtension()

然后它关闭内容扩展但不关闭它。

如果你打电话:

self.extensionContext?.performNotificationDefaultAction()

然后它完成操作并关闭该单个通知(而不是其他通知)。但是,这会打开应用程序,您可能不想这样做。

我的委托(delegate)是这样设置的:

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

    switch response.actionIdentifier {
    case UNNotificationDismissActionIdentifier:
        // Dismiss action stuff.
        completion(.dismiss)
        // To not dismiss
        // completion(.doNotDismiss)
    case UNNotificationDefaultActionIdentifier:
        // Default action stuff.
        // Let's say the user executed default action, we want to dismiss and forward the action.
        completion(.dismissAndForwardAction)
        break
    default:
        break
    }
}

关于ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48800313/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4