【发布时间】:2017-09-19 12:38:33
【问题描述】:
我已经设置了用户通知。他们交付得很好,但应用程序图标上的徽章始终是一个。 这是我的代码:
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)
}
})
}
知道为什么徽章总是显示一个并且不会随着通知的传递而增加吗?
谢谢
【问题讨论】:
-
这只是一个标签,而不是其他任何东西。 Apple 没有为本地通知提供徽章自动增量。
-
@Mannopson 所以基本上没有办法在通知发送时增加该标签?
-
但是您可以提供正确的值并将其设置为徽章。例如:
content.badge = deliveredNotifications.count或类似的东西(仅显示错过的通知计数) -
它只显示提供的值,在你的情况下它是一 (1)。
-
它不应该向 UIApplication.shared.applicationIconBadgeNumber 添加一个吗?这实际上是正确的......我的意思是如果我从委托(willPresent Notification)打印它的值是正确的
标签: ios swift unusernotificationcenter