Swift 4 UNUserNotificationCenter持续时间(过去24小时内收到通
发布时间:2020-12-14 05:30:42 所属栏目:百科 来源:网络整理
导读:我正在使用UNUserNotificationCenter来发送通知,如下所示: UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in self.array = notifications} 然后在viewWillDisappear上我清除applicationIconBadgeNumber,如下所示: over
我正在使用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小时内触发的通知.使用此功能:
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 } } } 并在AppDelegate的applicationWillResignActive(_ application :)中调用它 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |