当应用程序关闭时,从推送通知中打开特定的ViewController

时间:2019-07-03 11:51:15

标签: ios swift

在应用关闭时(不处于后台或非活动状态)推送通知时,我不会打开特殊的ViewController 当应用程序在后台运行时有效,但是当应用程序关闭时,Xcode推送到Main ViewController

我尝试使用所有应用程序状态,但不起作用

    if(application.applicationState == .background) {
        let dict = userInfo["aps"] as? NSDictionary
        if let cont = dict {
            let cate = cont["category"] as? String
            if let category = cate {
                switch category {
                case "NEWS":
                    let storyboard = UIStoryboard(name: "Shops", bundle: nil)
                    let NewBoard = storyboard.instantiateViewController(withIdentifier: "MainShopsViewController")
                    let tabbar = self.window?.rootViewController as? UITabBarController

                    let navcontrol = tabbar?.selectedViewController as? UINavigationController
                    tabbar?.selectedIndex = 1
                    navcontrol?.pushViewController(NewBoard, animated: true)
                    self.window?.makeKeyAndVisible()
                default:
                    break
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

您可以使用此功能执行此操作。

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    // tell the app that we have finished processing the user’s action / response
    completionHandler()
}

答案 1 :(得分:0)

使用此功能。

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
   if let userInfo = response.notification.request.content.userInfo as? [String : Any] {

        }

  UIApplication.shared.keyWindow?.rootViewController = yourViewController
  completionHandler()
}

答案 2 :(得分:0)

如果您想从应用程序的关闭状态打开return Stack( fit: StackFit.expand, children: <Widget>[ Image( image: image, fit: BoxFit.cover, ), Align( alignment: Alignment.topRight, child: Icon( Icons.delete, color: Colors.white, ), ), ], ); ,则可以使用vc的方法通过单击“通知”来检查应用程序是否已打开。

AppDelegate

它看起来像:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // When the app launch after user tap on notification (originally was not running / not in background) if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil { goToEventDetails(userInfo: launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any]) } } 是实例化VC和推送ID的方法,而goToEventDetails是Notification的内容。