Swift - getDeliveredNotifications with completionHandler gives an empty array on first notification

Viewed 2258

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!

0 Answers
Related