首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift UNUsernotification applicationIconBadgeNumber始终显示一个

Swift UNUsernotification applicationIconBadgeNumber始终显示一个
EN

Stack Overflow用户
提问于 2017-09-19 20:38:34
回答 1查看 1.3K关注 0票数 2

我已经设置了用户通知。他们得到了很好的交付,但应用程序图标上的徽章总是一个。下面是我的代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
 @IBAction func saveBtnPressed(sender: UIButton) {
    scheduleNotif()
}

下面是scheduleNotif函数

代码语言:javascript
复制
 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)
        }
    })
}

你知道为什么徽章总是显示一个,并且不会随着通知的发送而递增吗?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-09-19 20:56:34

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

代码语言:javascript
复制
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber

出自:

代码语言:javascript
复制
UIApplication.shared.applicationIconBadgeNumber += 1
content.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46300673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档