[英]Swift - Local notification icon badge number update on deliver App is in background
I am trying to figure how I can update dynamically the icon badge number when a local notification is delivered. 我试图弄清楚当发送本地通知时如何动态更新图标徽章编号。 Registering the badge number upon scheduling is not an option since if I register two or more notification before any are delivered the
按计划注册徽章号码不是一种选择,因为如果我在交付任何通知之前注册了两个或更多通知,
UIApplication.shared.applicationIconBadgeNumber // this will be zero
will always be zero until a notification is delivered. 在传递通知之前,它将始终为零。
I can use the UNUsernotification delegate with the func 我可以将UNUsernotification委托与func一起使用
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { }
but this function is called just if the app is active. 但是仅在应用程序处于活动状态时才调用此函数。 What if the app is not active?
如果该应用程序未激活怎么办?
I've read a bit around and pretty much everyone i've read says there is no way to do such a thing! 我已经阅读了一些,几乎所有阅读过的人都说没有办法做这样的事情! But is this even possible??
但这有可能吗?
How the Apple manage the notification for reminder and calendar? 苹果如何管理提醒和日历的通知? are they local notification and they update the icon badge?
他们是本地通知,并且他们更新了图标徽章? or am I making a mistake?
还是我做错了? I believe it must be a way to update the icon badge when the local notification is delivered?
我相信这是在传递本地通知时更新图标徽章的一种方法吗?
Any idea guys? 有想法吗? can't believe Apple did not provide a way to achieve this!
不能相信苹果没有提供实现这一目标的方法! Thank you!
谢谢!
UNMutableNotificationContent has a property call badge. UNMutableNotificationContent具有属性调用标记。 You set this property before trigger de notification and that's it!
您在触发通知之前设置了此属性,仅此而已! Badge number property is NSNumber so it's a little tricky incrementing it by 1.
徽章编号属性为NSNumber,因此将其递增1有点棘手。
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
"Your Last Time Alarm!", arguments: nil)
content.body = self.userInfo["descripcion"]!
content.sound = UNNotificationSound.default
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)
The rest is set the trigger and add the request: 其余的设置触发器,并添加请求:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: idNotificacion, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
if let theError = error {
print(theError)
} else {
...
}
}
In order to be able to increase the badge for both repetitive and scheduled notifications, you should increase UIApplication.shared.applicationIconBadgeNumber, before configuring the notification: 为了能够增加重复通知和计划通知的徽章,应在配置通知之前增加UIApplication.shared.applicationIconBadgeNumber:
UIApplication.shared.applicationIconBadgeNumber += 1
And then just simply: 然后只需:
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Test"
notificationContent.subtitle = "Test"
notificationContent.body = "Test"
notificationContent.sound = UNNotificationSound.default
notificationContent.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber
In order to reset the counter every time the user opens the app, just simply set the value of UIApplication.shared.applicationIconBadgeNumber to 0 in AppDelegate.swift like this: 为了在用户每次打开应用程序时重置计数器,只需将AppDelegate.swift中的UIApplication.shared.applicationIconBadgeNumber的值设置为0即可,如下所示:
func applicationWillResignActive(_ application: UIApplication) {
UIApplication.shared.applicationIconBadgeNumber = 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.