gpt4 book ai didi

ios - Swift UNUsernotification applicationIconBadgeNumber 始终显示一个

转载 作者:可可西里 更新时间:2023-11-01 02:01:03 26 4
gpt4 key购买 nike

我已经设置了用户通知。他们交付得很好,但应用程序图标上的角标(Badge)始终是一个。这是我的代码:

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)
}
})
}

知道为什么角标(Badge)将始终显示一个并且不会随着通知的传递而递增吗?

谢谢

最佳答案

我认为您需要增加 applicationIconBadgeNumber。所以替换

content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber

通过:

UIApplication.shared.applicationIconBadgeNumber += 1
content.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber

关于ios - Swift UNUsernotification applicationIconBadgeNumber 始终显示一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46300673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com