Swift UNUsernotification applicationIconBadgeNumber始终显示一个
问题描述:
我已设置用户通知。他们交付得很好,但应用程序图标上的徽章始终是一个。 这里是我的代码:Swift UNUsernotification applicationIconBadgeNumber始终显示一个
let center = UNUserNotificationCenter.current()
let options: UNAuthorizationOptions = [.alert, .badge, .sound];
center.requestAuthorization(options: options) {
(granted, error) in
if !granted {
// ask for permission
}
}
当使用单击按钮我安排的通知符:
@IBAction func saveBtnPressed(sender: UIButton) {
scheduleNotif()
}
这里的scheduleNotif功能
func scheduleNotif() {
let dateformatter = DateFormatter()
dateformatter.dateStyle = DateFormatter.Style.medium
dateformatter.timeStyle = DateFormatter.Style.short
let dateFromString = dateformatter.date(from: selectDateTextField.text!)
let fireDateOfNotification: Date = dateFromString!
//Notif are enabled
let content = UNMutableNotificationContent()
content.title = notifTitleTextField.text!
content.body = notifNoteTextView.text
content.sound = UNNotificationSound.default()
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber
var trigger: UNCalendarNotificationTrigger
var triggerDate = DateComponents()
trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
let titleNospace = notifTitleTextField.text?.replacingOccurrences(of: " ", with: "")
var identifier = titleNospace
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
self.center.add(request, withCompletionHandler: { (error) in
if let error = error {
print(error.localizedDescription)
}
})
}
任何想法,为什么徽章将始终显示一个并且在通知交付时永不增加?
谢谢
答
我认为你需要增加applicationIconBadgeNumber
。所以更换
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber
通过:
UIApplication.shared.applicationIconBadgeNumber += 1
content.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber
+0
如果你这样做,它会增加徽章,一旦预定。 ..而不是当它交付 – Marco
这只是一个标签,而不是别的。 Apple不会为本地通知提供徽章自动增量。 – Mannopson
@Mannopson所以基本上没有办法增加标签,因为通知交付? – Marco
但是,您可以提供正确的值并将其设置为徽章。例如:'content.badge = deliveredNotifications.count'或类似的东西(只显示错过的通知数) – Mannopson