Ios 执行按操作通知Swift
我使用Firebase通知发送和接收远程通知 当我收到通知时,我也会收到一个ID,让我能够识别一个特定的“帖子” 现在,当我单击notification时,我需要该应用程序打开一个viewController,将接收到的ID传递给它,以便启动一个加载“post”信息的方法(在Ios 执行按操作通知Swift,ios,swift,firebase,push-notification,firebase-cloud-messaging,Ios,Swift,Firebase,Push Notification,Firebase Cloud Messaging,我使用Firebase通知发送和接收远程通知 当我收到通知时,我也会收到一个ID,让我能够识别一个特定的“帖子” 现在,当我单击notification时,我需要该应用程序打开一个viewController,将接收到的ID传递给它,以便启动一个加载“post”信息的方法(在viewDidLoad()) 以下是通知代码: func application(_ application: UIApplication, didReceiveRemoteNotification userInfo
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:UIApplication,DidReceiveMemoteNotification用户信息:[AnyHashable:Any],
fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult)->Void{
//打印消息ID。
打印(“消息ID:\(userInfo[“gcm.Message\u ID”!))
让aps=userInfo[“aps”]作为!NSDictionary
让body_notifica=aps[“alert”]!as!NSDictionary
让titolo_notifica=body_notifica[“title”]!as!字符串
设testo_notifica=body_notifica[“body”]!as!字符串
让id\u lavoro=userInfo[“id\u lavoro”]!//这是我需要传递给视图控制器的id
如果application.applicationState==UIApplicationState.inactive | | application.applicationState==UIApplicationState.background{
让viewController=self.window!.rootViewController!.storyboard!。实例化viewController(标识符为:“annuncio\u view\u controller”)
viewController.PerformScheue(标识符为“a”,发送方为:)
}
let banner=banner(标题:titolo_notifica,副标题:testo_notifica,图片:UIImage(名为:“Info”),背景色:UIColor(红色:0.0/255.0,绿色:0.0/255.0,蓝色:0.0/255.0,阿尔法:0.5))
banner.dismissentap=true
旗帜秀(持续时间:3.0)
打印(用户信息)
}
谢谢你们 您可以使用这个代码示例来处理这个问题
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中)多亏了@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)
}
好啊但是如何设置该视图的ID?我需要一个类似的“prepare(for:segue)…
viewController.your_id=id\u lavoro
如果运行代码,会发生什么let viewController=self.window!.rootViewController!.storyboard!。instanceviewcontroller(带有标识符:“home\u view\u controller”)as!HomeViewController viewController.toPassId=id\u lavoro!viewController.performsgue(带有标识符:“segue\u annuncio\u profilo”,发件人:self)
不明白您在做什么,为什么要执行?