我正在使用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 :)中调用它
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。