我已经设置了用户通知。他们得到了很好的交付,但应用程序图标上的徽章总是一个。下面是我的代码:
let center = UNUserNotificationCenter.current()
let options: UNAuthorizationOptions = [.alert, .badge, .sound];
center.requestAuthorization(options: options) {
(granted, error) in
if !granted {
// ask for permission
}
}使用时,单击按钮I schedule the notif:
@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)
}
})
}你知道为什么徽章总是显示一个,并且不会随着通知的发送而递增吗?
谢谢
发布于 2017-09-19 20:56:34
我认为您需要递增applicationIconBadgeNumber。所以替换掉
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber出自:
UIApplication.shared.applicationIconBadgeNumber += 1
content.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumberhttps://stackoverflow.com/questions/46300673
复制相似问题