我正在使用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小时。完成这个?
答案 0 :(得分:2)
获取通知的date属性,仅清除过去24小时内触发的通知。使用此功能:
func 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
}
}
}
并在applicationWillResignActive(_ application:)
的{{1}}中调用