0

I am using the following code to display a number as badge at my apps icon:

func triggerNotification(iAmountToday: Int) {
  UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
    if granted {
      let content = UNMutableNotificationContent()
      content.badge = iAmountToday
      content.categoryIdentifier = "com.psv.localNotification"
      let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1.0, repeats: false)
      let request = UNNotificationRequest.init(identifier: "AmountTodayUpdate", content: content, trigger: trigger)

      let center = UNUserNotificationCenter.currentNotificationCenter()
      center.addNotificationRequest(request, withCompletionHandler: { (error) in
        if (error != nil) {
          print (error)
        }
      })
    }
  }
}

Although no error is thrown, the badge is never shown at the apps icon.

What am I doing wrong?

Cheers

Amit Srivastava
  • 1,105
  • 9
  • 18
AntonSack
  • 1,021
  • 2
  • 25
  • 47

1 Answers1

2

Swift 3.0, iOS 10.

You should replace:

 UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in

with:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in

This worked for me.

Best regards Fabio