8

I have this code:

let center = UNUserNotificationCenter.current()

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    center.getDeliveredNotifications { (delivered) in
        print(delivered.count)
    }
    completionHandler([.alert, .badge])
}

I am trying to figure out how come delivered is an empty array when the first notification is delivered. Meaning if have for example, 2 notification scheduled, when the first one arrives it prints (0) and then when the second one arrives it prints (1).

Does any one understands why of such a behaviour? I'm trying to get the notification count as soon as they are delivered. Thank you!

Marco
  • 1,051
  • 1
  • 17
  • 40
  • What are you trying to do with this number? I suppose you're rather looking for `application.applicationIconBadgeNumber` if you want to get the notification count? – emmics Sep 20 '17 at 13:12
  • @Muli i gave up with the application.applicationIconBadgeNumber... apparently there is no way to auto increment the badge for local notification... if the app in in background it won't work! so i'm trying to get all the delivered notification in order to display to the user (when he opens the app) the unread notification in the section... – Marco Sep 20 '17 at 13:15
  • I'm struggling with this very thing right now. It's as if the OS sometimes fails to queue up notifications when the app is not running. – piccy Sep 22 '17 at 13:14
  • @piccy apparently it just does not work! i believe it might be a problem of the notification center... i gave up and used the date of the notification... is not the optimal solution.. but at least it works.. – Marco Sep 22 '17 at 13:15

0 Answers0