如何检测“清除"通知 [英] How to detect "clear" notifications

查看:33
本文介绍了如何检测“清除"通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果从用户通知到达通知中心的时间超过一分钟,则有一个清除"选项可以从通知中心一次性关闭一个或多个通知.

if more than a minute pass from the time a user notification arrived to notification center, there is a "clear" option to dismiss one or more notifications at once from notification center.

iOS 操作系统如何通知用户点击清除"以同时关闭多个通知?

How the iOS OS notify that the user tapped on "clear" to dismiss several notifications together?

推荐答案

Van 的 anwser 直奔正确的方向,但我们不需要实现自定义操作来获得提问者想要的东西.

Van's anwser goes straight into the right direction, but we do not need to implement the custom action to get what the question giver wanted.

如果您创建类别并将其传递给 UNUserNotificationCenter,即使用户选择了内置清除按钮或内容扩展上的X"按钮,您也会收到委托 didReceive 函数的回调.然后 ResponeIdentifier 将是 response.actionIdentifier == UNNotificationDismissActionIdentifier.

If you create the category and pass it to the UNUserNotificationCenter you get a callback on the delegates didReceive function even if the user tabbed on the builtin Clear Button or the "X" Button on the content extension. The ResponeIdentifier will then be response.actionIdentifier == UNNotificationDismissActionIdentifier.

类别必须是这样的:

//Create the category...
UNNotificationCategory(identifier: "YourCustomIdentifier",
actions: [], intentIdentifiers: [], options: .customDismissAction)

//... and pass it to the UNUserNotificationCenter
UNUserNotificationCenter.current().setNotificationCategories(notificationCategories)

类别在 iOS 框架中触发了魔法,突然间你在你的委托中得到了回调.委托函数应如下所示:

The category triggers the magic in the iOS framework and suddenly you get callbacks in your delegate. The delegate function should look like:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                        didReceive response: UNNotificationResponse,
                        withCompletionHandler completionHandler: @escaping () -> Void) {
  if response.actionIdentifier == UNNotificationDismissActionIdentifier {
    // notification has been dismissed somehow        
  }
  completionHandler()
}

这篇关于如何检测“清除"通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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