2017-09-19 6 views
1

私はユーザー通知を設定しました。彼らはうまくいきますが、アプリアイコンのバッジは常に1つです。ここ は私のコードです:Swift UNUnnnnnn通知アプリケーションIconBadgeNumberは常に1を表示します

let center = UNUserNotificationCenter.current() 
let options: UNAuthorizationOptions = [.alert, .badge, .sound]; 

center.requestAuthorization(options: options) { 
     (granted, error) in 
     if !granted { 
      // ask for permission 
    } 
} 

使用は、私が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) 
     } 
    }) 
} 

さんのバッジは常に1が表示されますなぜすべてのアイデアを通知が配信されると増分しませんか?

は、私はあなたがapplicationIconBadgeNumberをインクリメントする必要があると思うあなた

+0

:だからでは

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

を交換してください。アップルはローカル通知のバッジ自動増分を提供していません。 – Mannopson

+0

@Mannopsonなので、基本的に、通知が届いたときにそのラベルを増やす方法はありませんか? – Marco

+0

ただし、正しい値を指定してバッジとして設定することができます。例: 'content.badge = deliveredNotifications.count'またはそのようなもの(届かなかった通知の数だけを表示) – Mannopson

答えて

1

ありがとうございます。それはただのラベルではなく、何だ

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

そうすれば、スケジューリングされるとすぐにバッジがインクリメントされます。 ..それが配信されたときではなく – Marco

関連する問題