1

I want to show alert when notification is fired while app is running, here is tutorial which i used for local notifications. In tutorial, it uses UIAlertView to show alert and it works, here is code:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    // Point for handling the local notification when the app is open.
    // Showing reminder details in an alertview
    UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
}

but it gives warning that UIAlertView is deprecated in iOS 9, so i used UIAlertController, but when i run it it gives warning:

Attempt to present <UIAlertController: 0x7c322a00> on <xxx> whose view is not in the window hierarchy!

Here is my didReceiveLocalNotification method:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in
    }))

    self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}

How i can show UIAlertController in didReceiveLocalNotification method? I also tried:

 let activeViewCont = application.windows[0].rootViewController?.presentedViewController
 activeViewCont!.presentViewController(alert, animated: true, completion: nil)
kakajan
  • 2,614
  • 2
  • 22
  • 39
  • `self.window.rootViewController` is this a navigation controller in your project? – Nishant Feb 27 '16 at 07:30
  • @Nishant no, i didn't use navigation controller on my project. `self.window.rootViewController` is returns my SplashScreen where i created my own splash screen, it downloads data when app is opening for first time. But i need to get current showing view controller – kakajan Feb 27 '16 at 07:33

1 Answers1

9

Try this:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    var topController : UIViewController = (application.keyWindow?.rootViewController)!

    while ((topController.presentedViewController) != nil) {

        topController = topController.presentedViewController!
    }

    let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in}))

    topController.presentViewController(alert, animated: true, completion: nil)

})

Hope it helps.

Nishant
  • 12,529
  • 9
  • 59
  • 94
  • i also tried this before, it does not help too. it gives same error: `Warning: Attempt to present on whose view is not in the window hierarchy!` – kakajan Feb 27 '16 at 07:47
  • I think i need to find current showing view controller, but i don't know how, for example, when i get that warning, app wasn't showing splash screen, it was showing another view controller – kakajan Feb 27 '16 at 07:48
  • thanks the solution in link which you provided solved my issue – kakajan Feb 27 '16 at 08:01
  • @kakajan: great. I have modified my code to the right answer. – Nishant Feb 27 '16 at 08:10
  • thanks for help, i accepted and upvote your answer ;) – kakajan Feb 27 '16 at 08:11
  • anyway this is a working solution great, but can you please summarize exactly why the op's getting warning and me too, plus one for you. – vaibhav Dec 09 '16 at 11:37