Mar*_*rco 5 ios uilocalnotification swift unusernotificationcenter
我试图弄清楚如何在传递本地通知时动态更新图标徽章编号。在安排时注册徽章号码不是一个选项,因为如果我在发送任何通知之前注册两个或多个通知,
UIApplication.shared.applicationIconBadgeNumber // this will be zero
Run Code Online (Sandbox Code Playgroud)
在发送通知之前将始终为零。
我可以将 UNUsernotification 委托与 func 一起使用
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { }
Run Code Online (Sandbox Code Playgroud)
但仅当应用程序处于活动状态时才会调用此函数。如果应用程序未激活怎么办?
我读过一些书,几乎我读过的每个人都说没有办法做这样的事情!但这可能吗?
苹果如何管理提醒和日历的通知?他们是本地通知并更新图标徽章吗?或者我犯了一个错误?我相信这一定是在发送本地通知时更新图标徽章的一种方法?
大家有什么想法吗?不敢相信苹果没有提供实现这一目标的方法!谢谢你!
UNMutableNotificationContent 有一个属性调用标记。您可以在触发通知之前设置此属性,仅此而已!徽章编号属性是 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)
Run Code Online (Sandbox Code Playgroud)
剩下的就是设置触发器并添加请求:
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 {
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4214 次 |
| 最近记录: |