【问题标题】:Perform action pressing notification Swift执行动作按下通知 Swift
【发布时间】:2016-11-27 10:48:55
【问题描述】:

我正在使用 Firebase 通知来发送和接收 RemoteNotification。

当我收到通知时,我还会收到一个 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)

}

谢谢你们!

【问题讨论】:

    标签: ios swift firebase push-notification firebase-cloud-messaging


    【解决方案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)
    
    
        }
    

    【讨论】:

      【解决方案2】:

      您可以使用此代码示例来处理此问题

      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 中)

      【讨论】:

      • 好的。但是如何将 ID 设置为该视图?我需要一个类似的“prepare(for: segue)...
      • viewController.your_id = id_lavoro
      • 如果你运行你的代码会发生什么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)
      • 不明白你在做什么,为什么要执行Segue??为什么不推动或呈现?
      猜你喜欢
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 2016-10-13
      • 1970-01-01
      • 2017-04-29
      • 2016-03-18
      • 1970-01-01
      相关资源
      最近更新 更多