我使用以下代码在我的应用图标上显示一个数字作为徽章:
func triggerNotification(iAmountToday: Int) {
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
if granted {
let content = UNMutableNotificationContent()
content.badge = iAmountToday
content.categoryIdentifier = "com.psv.localNotification"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1.0, repeats: false)
let request = UNNotificationRequest.init(identifier: "AmountTodayUpdate", content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request, withCompletionHandler: { (error) in
if (error != nil) {
print (error)
}
})
}
}
}
虽然没有引发任何错误,但徽章永远不会显示在应用图标上。
我做错了什么?
干杯
答案 0 :(得分:2)
Swift 3.0,iOS 10。
你应该替换:
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
使用:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
这对我有用。
祝你好运 法比奥