我试图弄清楚如何在传递本地通知时动态更新图标徽章编号。
在安排时注册徽章号码不是一个选项,因为如果我在发送任何通知之前注册两个或多个通知,
UIApplication.shared.applicationIconBadgeNumber // this will be zero
在发送通知之前将始终为零。
我可以将 UNUsernotification 委托与 func 一起使用
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { }
但仅当应用程序处于活动状态时才会调用此函数。
如果应用程序未激活怎么办?
我读过一些书,几乎我读过的每个人都说没有办法做这样的事情!但这可能吗?
苹果如何管理提醒和日历的通知?他们是本地通知并更新图标徽章吗?或者我犯了一个错误?我相信这一定是在发送本地通知时更新图标徽章的一种方法?
大家有什么想法吗?不敢相信苹果没有提供实现这一目标的方法!谢谢你!
为了能够增加重复通知和计划通知的徽章,您应该在配置通知之前增加 UIApplication.shared.applicationIconBadgeNumber:
UIApplication.shared.applicationIconBadgeNumber += 1
然后简单地说:
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Test"
notificationContent.subtitle = "Test"
notificationContent.body = "Test"
notificationContent.sound = UNNotificationSound.default
notificationContent.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber
为了在用户每次打开应用程序时重置计数器,只需在 AppDelegate.swift 中将 UIApplication.shared.applicationIconBadgeNumber 的值设置为 0,如下所示:
func applicationWillResignActive(_ application: UIApplication) {
UIApplication.shared.applicationIconBadgeNumber = 0
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)