0

I'm trying to present the message from a Push Notification(PN) in a UIAlertController from AppDelegate to the current ViewController. If I send 1 PN where is no problem and the alert is displayed! But then I send a second PN before I clicked on the OK button from the alert, the message is not displayed and get the following warning message:

"Warning: Attempt to present <UIAlertController> on <NavigationViewController> which is already presenting <UIAlertController>"

So how can I handle more than 1 PN or is it possible to show the last PN?

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        guard let aps = userInfo["aps"] as? [String: AnyObject] else {
            completionHandler(.failed)
            return
        }
        dump(aps)
        let message = aps["alert"] as? String
        let alertController = UIAlertController(title: "New message", message: message, preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(ok)

        DispatchQueue.main.async {
            self.window?.rootViewController?.presentedViewController?.present(alertController, animated: true, completion: nil)
        }

        completionHandler(UIBackgroundFetchResult.noData)
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
egram
  • 1
  • 1

1 Answers1

0

You could manage a list/stack of notifications and display them consecutively till your list/stack is empty.

Take a look at this answer for how to display multiple alerts: Attempt to present UIAlertController on View Controller which is already presenting (null) [Swift]

Jeremy Merse
  • 81
  • 1
  • 3