DeyaEldeen
DeyaEldeen

Reputation: 11807

reading latest push notification data when the app is brought back to foreground (not by tapping notification)

when implementing push notifications the user can tap the notification to bring a minimized app into foreground, this works fine with me, and I read the notification data normally like this...

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    let userInfo = notification.request.content.userInfo
    parsePayload(arg: (userInfo["PN_Body"] as? NSString)!)
    completionHandler([.alert, .badge, .sound])
}

my question is about another scenario

what if the user received say 5 notifications while the app is minimized, and and brought the app into foreground (not by tapping the notification), then how would I read the latest notification that was delivered?

Upvotes: 0

Views: 345

Answers (1)

JustinM
JustinM

Reputation: 2222

You can get the notifications that are still visible in the notification center by calling func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)

"notifications: An array of UNNotification objects representing the local and remote notifications of your app that have been delivered and are still visible in Notification Center. If none of your app’s notifications are visible in Notification Center, the array is empty."

However, this may not provide accurate info if the notifications are stale and have been sitting in the notification center for a while. You can make it real messy and start checking delivered date on notification and compare it against the last time the app entered background/terminated.

As @paulw11 said, you should check with server and refresh info that way.

Upvotes: 1

Related Questions