Show uiviewcontroller from remote notification when app is closed

I am trying to show a specific viewcontroller when I receive a remote push notification. I added all my code to the method didReceiveRemoteNotification

:

func application(application: UIApplication, didReceiveRemoteNotification userinfo: [NSObject: AnyObject])

      

I added the following code:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let code = (userInfo["aps"] as! [String: AnyObject])
    // Call to retrieve blog
    if let blog = code["b"] as? NSNumber {
        let blogId = blog as! Int

        // Show blog from notification
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
        var controller = mainStoryboard.instantiateViewControllerWithIdentifier("blogCtrl") as! BlogController
        controller.blogId = blogId
        var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
        self.window?.rootViewController = rootController
        rootController.pushViewController(controller, animated: true)
        self.window?.makeKeyAndVisible()
    }
    if let tonic = code["t"] as? NSNumber {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
        var controller = mainStoryboard.instantiateViewControllerWithIdentifier("tonicDetail") as! TonicDetailController
        controller.tonicId = tonic as! Int
        var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
        self.window?.rootViewController = rootController
        rootController.pushViewController(controller, animated: true)
        self.window?.makeKeyAndVisible()
    }
    if let gin = code["g"] as? NSNumber {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
        var controller = mainStoryboard.instantiateViewControllerWithIdentifier("GinDetail") as! GinDetailController
        controller.ginId = gin as! Int
        var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
        self.window?.rootViewController = rootController
        rootController.pushViewController(controller, animated: true)
        self.window?.makeKeyAndVisible()
    }
}

      

When the app is in the background everything works, but when the app is sent and I receive a remote notification, it only starts the app. Is there a method that can be called if the application has been submitted before?

+3


source to share


1 answer


When your application is launched after exiting the application, at that time you received the remote notification, then didFinishLaunchingWithOptions

this is the first method in the AppDelegate that starts the application launch, checks if you received any notification or not, and do your job accordingly.

You should look for this method in your application delegate: -

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

      



Now check if your app received any push notification or not

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (notification) {
        NSLog(@"app received a notification %@",notification);
        [self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
    }else{
        NSLog(@"app did not receive a notification");
    }

      

+2


source







All Articles