我正在使用UNUserNotificationCenter来发送通知,如下所示:
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in self.array = notifications }
然后在viewWillDisappear上我清除applicationIconBadgeNumber,如下所示:
override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) UIApplication.shared.applicationIconBadgeNumber = 0 }
这样做不会让我的通知持续很长时间,是的,我希望看到通知后徽章编号为0,但我希望这些会持续24到48小时….我怎么能做到这一点?
获取通知的日期属性,并仅清除过去24小时内触发的通知.使用此功能:
原文链接:https://www.f2er.com/swift/319425.htmlfunc updateAppIcon() { UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in let past24hNotifications = notifications .filter { $0.date > Date().addingTimeInterval(-24 * 60 * 60)} DispatchQueue.main.async { UIApplication.shared.applicationIconBadgeNumber = past24hNotifications.count } } }
并在AppDelegate的applicationWillResignActive(_ application :)中调用它