How to go to the view manager using push notification

I want to go to a specific view controller after receiving a push notification. After navigation, the navigation stack should work as if the user had reached the view manually.

Storyboard: http://www.xpos.nl/xpos/images/common/storyboard.png

In AppDelegate.swift, I already have:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { println("didReceiveRemoteNotification") let storyboard = UIStoryboard(name: "Main", bundle: nil) let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController let navigationController = self.window?.destinationViewController; navigationController?.pushViewController(destinationViewController, animated: false, completion: nil) } 

But I get an error that destinationViewController is not part of the window, or if I fix it (try other answers on stackoverflow), nothing happens.

+5
source share
2 answers

TargetViewController is not part of the window because it has not been added, just initialized. Based on the assumption that the ViewController is your rootViewController, click on your destinationViewController as follows:

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { println("didReceiveRemoteNotification") let storyboard = UIStoryboard(name: "Main", bundle: nil) let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController let navigationController = self.window?.rootViewController as! UINavigationController navigationController?.pushViewController(destinationViewController, animated: false, completion: nil) } 

Optional: To go from "Bestellen" to "MessageViewController" and then click on "Berichten", you also need to click on all the other viewControllers between the two. There is no built-in function or algorithm for this.

+5
source

try it

  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { println("didReceiveRemoteNotification") let storyboard = UIStoryboard(name: "Main", bundle: nil) let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController self.window?.rootViewController?.presentViewController(destinationViewController, animated: True, completion:nil) } 
+2
source

Source: https://habr.com/ru/post/1216164/


All Articles