Currently, I am trying to get an event to trigger after a user has received a notification. Notifications are appearing at the correct time. However, the function that checks for delivered notifications never shows any delivered notifications. I have verified that the code is running, it always prints 0 for the count of delivered notifications.
This is how I am checking for delivered notifications:
UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: { deliveredNotifications -> () in
print(deliveredNotifications.count)
})
And this is how I'm creating notifications:
let content = UNMutableNotificationContent()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d yyyy, hh:mm a"
let date:Date = dateFormatter.date(from: date)!
content.body = "Notification occurred"
content.sound = UNNotificationSound.default()
let timeInterval = date.timeIntervalSinceNow
if timeInterval > 0.0 {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval:
timeInterval, repeats: false)
let request = UNNotificationRequest(identifier: id, content:
content, trigger: trigger)
UNUserNotificationCenter.current().add(request,
withCompletionHandler: nil)
}
How can I get a portion of code to execute after a notification has been delivered?