执行按通知的操作Swift

问题描述:

我正在使用Firebase通知来发送和接收RemoteNotification。执行按通知的操作Swift

当我收到通知时,我还收到一个ID,可以让我确定一个特定的“帖子”。

现在,当我点击通知时,我需要该应用程序打开一个viewController,将它传递给收到的ID,以启动加载“发布”信息的方法(在viewDidLoad())。

这是通知中的代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    let aps = userInfo["aps"] as! NSDictionary 
    let body_notifica = aps["alert"]! as! NSDictionary 
    let titolo_notifica = body_notifica["title"]! as! String 
    let testo_notifica = body_notifica["body"]! as! String 

    let id_lavoro = userInfo["id_lavoro"]! //THIS IS THE ID I NEED TO PASS TO VIEW CONTROLLER 

    if application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background{ 
     let viewController = self.window!.rootViewController!.storyboard!.instantiateViewController(withIdentifier: "annuncio_view_controler") 
     viewController.performSegue(withIdentifier: "a", sender: <#T##Any?#>) 
    } 


    let banner = Banner(title: titolo_notifica, subtitle: testo_notifica, image: UIImage(named: "Info"), backgroundColor: UIColor(red:0.0/255.0, green:0.0/255.0, blue:0.0/255.0, alpha:0.5)) 
    banner.dismissesOnTap = true 
    banner.show(duration: 3.0) 

    print(userInfo) 

} 

感谢你们!

您可以通过使用这个代码示例处理这种

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 


    if (application.applicationState == .inactive || application.applicationState == .background){ 

     //handle tapping on push notification and here you can open needed controller 
    } 
} 

确切地知道你需要打开哪个控制器需要看到控制器层次

在我的情况下,其

`guard let rootViewController = self.window?.rootViewController as? TabBarVC else { 
     return 
    } 
    // select second tab 
    rootViewController.selectedIndex = 1 

    guard let navigationController = rootViewController.selectedViewController as? UINavigationController else { 
     return 
    } 

`

编辑:我试过这个,但没有。

if application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background{ 
     let viewController = self.window!.rootViewController!.storyboard!.instantiateViewController(withIdentifier: "home_view_controller") as! HomeViewController 

     viewController.toPassId = id_lavoro! 
     viewController.performSegue(withIdentifier: "segue_annuncio_profilo", sender: self) 

    } 

如何检查层次结构?我的看法是没有的TabBar的视图,它可以从大量的视图中打开(即还停留在的TabBar)

+0

确定。但我怎样才能将ID设置为该视图?我需要一个类似的“准备(for:segue)... –

+0

'viewController.your_id = id_lavoro' –

+0

如果你运行你的代码,我已经编辑了问题添加代码 –

由于@Vadim科扎克

if application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background{ 
     guard let rootViewController = self.window?.rootViewController as? UITabBarController else { 
      return 
     } 
     // select second tab 
     rootViewController.selectedIndex = 1 

     guard let navigationController = rootViewController.selectedViewController as? UINavigationController else { 
      return 
     } 


     let viewController = self.window!.rootViewController!.storyboard!.instantiateViewController(withIdentifier: "annuncio_view_controler") as! LavoroViewController 

     viewController.id_lavoro = id_lavoro! 

     rootViewController.present(viewController, animated: true, completion: nil) 


    }