繁体   English   中英

在推送通知Swift 2 AppDelegate上加载特定的viewController

[英]Load a specific viewController on push notification Swift 2 AppDelegate

我想打开一个特定的视图控制器(一个隐藏的视图控制器,因为它只有在收到某个推送通知时才可以访问(不能通过选项卡栏访问)),我已经能够做到这一点,但是我遇到了一个问题。 。

我的应用程序有一个名为RootViewController的自定义选项卡栏控制器。 当您单击具有特殊值的通知时,它将显示一个警报视图,询问用户是否要打开通知。

通知触发将特定的视图控制器置于最前面,但问题是我不再有权访问选项卡栏。

我不知道该如何实现。

这是我在AppDelegate.m代码:

var presentedVC = self.window?.rootViewController as? UINavigationController
while (presentedVC!.navigationController != nil)  {
    presentedVC = presentedVC!.navigationController
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("PushNotificationView") as? NotTestViewController

destinationViewController?.url = self.url!
presentedVC?.presentViewController(destinationViewController!, animated: true, completion: nil)

})
)
alertCtrl.addAction(UIAlertAction(title: "Cancelar", style: .Destructive, handler: nil))

该代码有效,但不适用于所需的行为。

有什么想法我想念的吗?

非常感谢

编辑

我将RAMTabBarAnimationController更改为TabBarController因为RAMTabBarAnimationController不会从TabBarController继承。 但是我仍然看到相同的行为。

我想回答我自己的问题,因为我认为也许有人正面临着同样的情况,我必须感谢@Muneeba

好吧,首先,您必须知道您的远程推送通知是否需要进行后台获取,这很重要,因为如果这样,那么didReceiveRemoteNotification被调用两次(第一次单击通知警报,第二次打开应用程序),因此,您必须意识到这一点。

就我而言,我没有进行后台获取,因此我的content-available:false标志设置为false。

接下来要做的是将您想要的["key":value]到通知有效负载中,以了解您是否要打开特定的视图控制器。

管理是否打开视图控制器。

在我的应用中,我想打开一个警报控件,该弹出窗口始终显示推送通知的主体(带有“确定”按钮),并且仅当设置了某些值时,才打开一个警报控件,询问用户是否要打开到达的新内容(通常,该内容是基于Web的内容,可以打开嵌入在视图控制器中的Webview)

didReceiveRemoteNotification

if let mensaje = userInfo["m"] as? String {
// Here is where I know if the value is set in the notification payload
 let alertCtrl = UIAlertController(title: titulo, message: mensaje as String, preferredStyle: UIAlertControllerStyle.Alert)
 if url == nil {
   alertCtrl.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
 } else {
 alertCtrl.addAction(UIAlertAction(title: "Ver receta", style: .Default, handler: {
  action in

      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      //self.window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier("TabbedController") as! UITabBarController
      let navigationController = storyboard.instantiateViewControllerWithIdentifier("pushNotificationNavigation") as! UINavigationController
      let dVC:NotTestViewController = navigationController.topViewController as! NotTestViewController
      // This is for passing the URL to the view Controller
      dVC.url = self.url!
      self.window?.rootViewController?.presentViewController(navigationController, animated: true, completion: {})

      })
      )
      alertCtrl.addAction(UIAlertAction(title: "Cancelar", style: .Destructive, handler: nil))
   }
   // Find the presented VC...
   var presentedVC = self.window?.rootViewController
   while (presentedVC!.presentedViewController != nil)  {
      presentedVC = presentedVC!.presentedViewController
      }
      presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)

      handler(UIBackgroundFetchResult.NoData)
 }

您的RAMAnimatedTabBarController继承哪个类? 如果它继承了UIViewController / UITabbarController则使用UINavigationController并将其rootViewController设置为您的RAMAnimatedTabBarController实例,然后将其分配为窗口的rootViewController,并将此UINavigationController隐藏的navigationBar设置为隐藏,并且当您要推送到特定Controller时,请像这样

var presentedVC = self.window?.rootViewController as? UINavigationController
presentedVC(yourdesiredController, animated: true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM