我使用Firebase通知来发送和接收RemoteNotification。
当我收到通知时,我还会收到一个ID,让我可以识别特定的"帖子"。
现在,当我点击通知时,我需要该应用程序打开一个viewController,传递收到的ID,以便启动一个方法(在viewDidLoad()
中)加载" post"信息。
这是通知代码:
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)
}
谢谢你们!
答案 0 :(得分:1)
您可以使用此代码示例
来处理此问题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中)
答案 1 :(得分:1)
感谢@Vadim Kozak
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)
}