如何在应用程序被杀或未运行时从IOS应用程序的推送通知中打开特定视图?

时间:2018-05-07 21:07:42

标签: ios swift amazon-web-services firebase

当我收到通知时,我很难找到打开我的IOS应用程序的特定视图的解决方案,我想在用户点击收到的通知时打开一个屏幕。

我能够收到通知,当应用程序处于后台时我点击通知它会将我重定向到特定视图,当应用程序处于活动状态时它也会起作用但它只是在我点击它和应用程序时打开应用程序处于死亡状态..

下面是我的appdelegate代码

if application.applicationState == .inactive{

            print ("app is NOT active from not sec.")

            let articleId = userInfo["notification_id"] as? String

            UserDefaults.standard.set(articleId, forKey:"articleId");

            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "notificationsPageSB") as UIViewController
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = initialViewControlleripad
            self.window?.makeKeyAndVisible()

            let isPromoFCM = 1 as? Int

            UserDefaults.standard.set(isPromoFCM, forKey:"isPromoFCM1");

        }
        else if application.applicationState == .active {

            let articleId = userInfo["notification_id"] as? String

            UserDefaults.standard.set(articleId, forKey:"articleId");

            print ("app is active from not sec.")
            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "notificationsPageSB") as UIViewController
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = initialViewControlleripad
            self.window?.makeKeyAndVisible()


            /*     let storyboard = UIStoryboard(name: "Main", bundle: nil);
             let viewController: wow_Request_completed_vc = storyboard.instantiateViewController(withIdentifier: "wow_Request_completed_vc") as! wow_Request_completed_vc;

             // Then push that view controller onto the navigation stack
             let rootViewController = self.window!.rootViewController as! UINavigationController;
             rootViewController.pushViewController(viewController, animated: true);


             self.window?.rootViewController!.performSegue(withIdentifier: "link_to_wow_completed_vc", sender: nil)
             */



                //let isOpenedThroughFCM = "yes"

            //UserDefaults.standard.set(isOpenedThroughFCM, forKey:"isOpenedThroughFCM");

            isComingFromFCM3 = 1;


        } else {

            // let isOpenedThroughFCM = "yes"

  //              UserDefaults.standard.set(isOpenedThroughFCM, forKey:"isOpenedThroughFCM");
            isComingFromFCM3 = 1;
            print ("app is NOT active from not sec.")

            let articleId = userInfo["notification_id"] as? String

            UserDefaults.standard.set(articleId, forKey:"articleId");

            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "notificationsPageSB") as UIViewController
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = initialViewControlleripad
            self.window?.makeKeyAndVisible()

        }

2 个答案:

答案 0 :(得分:0)

实现这一目标的唯一方法是,我知道唯一有效的方法是检查委托方法中的launchOption键。

    if (launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]) != nil {
        // check the userInfo and take the needed action
    }

答案 1 :(得分:0)

根据我的想法,让我们尝试检查

中的[UIApplication sharedApplication].applicationIconBadgeNumber
  
      
  • (void)applicationDidBecomeActive:(UIApplication *)application
  •   

你可以确定导航到某处或某事

更新:我的答案是Objective-C,但您可以申请Swift:p