swift 如何检测/识别用户何时从iOS中的NotificationCenter托盘中清除所有推送通知

xvw2m8pv  于 2023-03-22  发布在  Swift
关注(0)|答案(1)|浏览(92)

我们可以检测/识别用户是否明确地清除每个推送通知。

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

但是有没有办法当用户从通知中心托盘中一次清除所有推送通知时。
任何人的任何帮助都非常感谢。
我已经尝试为每个推送通知设置UNNotificationCategory,但仍然无法在

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

用于一次清除所有推送通知。

lawou6xi

lawou6xi1#

没有直接的方法来检测用户是否已经清除了所有通知。
但是你可以尝试如下的变通方法。
下面的函数给予已发送通知的计数。

func hasUserClearedNotifications() {
    UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
        let deliveredCount = notifications.count
    }
}

你必须在你的应用程序中定期检查这个计数。如果计数变成零或者计数突然下降,那么用户很有可能已经清除了通知。
(请注意,这是一种变通方法。不是最佳实践)

相关问题