jjzjj

ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption

coder 2024-01-28 原文

我正在设置多个本地通知,并且我已经设置了通知操作数。喜欢小睡,表演,关闭。

    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。

最佳答案

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

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

如果你打电话

self.extensionContext?.dismissNotificationContentExtension()

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

如果你调用:

self.extensionContext?.performNotificationDefaultAction()

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

我的代表是这样设置的:

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/

有关ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption的更多相关文章

随机推荐