如何检测“清除”通知

elk*_*orb 8 apple-push-notifications ios swift

如果从用户通知到达通知中心的时间过去了超过一分钟,则有一个“清除”选项可以一次从通知中心取消一个或多个通知。

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

Kne*_*cht 5

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

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

类别必须是这样的:

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

//... and pass it to the UNUserNotificationCenter
UNUserNotificationCenter.current().setNotificationCategories(notificationCategories)
Run Code Online (Sandbox Code Playgroud)

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

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

  • 如果您清除单个通知,@KnechtRootrecht userNotificationCenter 会被调用,但如果您使用 x/clear 按钮一次清除所有通知,则不会调用此方法。至少在我这边是行不通的。 (5认同)
  • 这个 ```options: .customDismissAction``` 是让委托触发 ```UNNotificationDismissActionIdentifier``` 的关键。谢谢! (2认同)

Van*_*Van 0

从 iOS 10 及更高版本开始可以实现自定义通知,您需要使用 UNNotificaitons

private func registerForRemoteNotificaiton(_ application: UIApplication) {
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {(granted, error) in
                if granted {
                    DispatchQueue.main.async(execute: {
                        UIApplication.shared.registerForRemoteNotifications()
                    })
                }
        })
        configureUserNotification()
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        // For iOS 10 data message (sent via FCM)
        Messaging.messaging().delegate = self as MessagingDelegate
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    // [END register_for_notifications]
}

private func configureUserNotification() {
    if #available(iOS 10.0, *) {
        let action = UNNotificationAction(identifier: UNNotificationDismissActionIdentifier, title: "Cancel", options: [])
        //let action1 = UNNotificationAction(identifier: "dismiss", title: "OK", options: [])
        let category = UNNotificationCategory(identifier: "myNotificationCategory", actions: [action], intentIdentifiers: [], options: .customDismissAction)
        UNUserNotificationCenter.current().setNotificationCategories([category])
    } else {
        // Fallback on earlier versions
    }

}
Run Code Online (Sandbox Code Playgroud)

从 appdelegate 的 didFinishLaunching 方法调用 registerForRemoteNotificaiton 方法。

然后你需要在appdelegate中实现UNUserNotificationCenterDelegate。

然后你就会明白(这里是我们在操作名称中添加的“关闭”)

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.actionIdentifier == UNNotificationDismissActionIdentifier {
        //user dismissed the notifcaiton:

    }

    completionHandler()
}
Run Code Online (Sandbox Code Playgroud)

在这里找到更多信息