简体   繁体   English

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

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

I want to open a specific view controller (a hidden one, because it's only accessible when receiving a certain push notification (it's not accessible through the tab bar)) and I've been able to do this but I'm facing a problem.. 我想打开一个特定的视图控制器(一个隐藏的视图控制器,因为它只有在收到某个推送通知时才可以访问(不能通过选项卡栏访问)),我已经能够做到这一点,但是我遇到了一个问题。 。

My app has a custom Tab Bar controller called RootViewController . 我的应用程序有一个名为RootViewController的自定义选项卡栏控制器。 When you click on a notification with the special value it shows an alert view asking whether the user wants to open the notification or not. 当您单击具有特殊值的通知时,它将显示一个警报视图,询问用户是否要打开通知。

The notification triggers bringing a specific view controller to the front but the problem is that I don't have access to the tab bar anymore. 通知触发将特定的视图控制器置于最前面,但问题是我不再有权访问选项卡栏。

I don't know how to achieve this. 我不知道该如何实现。

This is my code in AppDelegate.m : 这是我在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))

This code works but not with the desired behaviour. 该代码有效,但不适用于所需的行为。

Any ideas what I am missing? 有什么想法我想念的吗?

Thank you so much 非常感谢

EDIT 编辑

I've changed the RAMTabBarAnimationController to TabBarController because RAMTabBarAnimationController does not inherit from TabBarController . 我将RAMTabBarAnimationController更改为TabBarController因为RAMTabBarAnimationController不会从TabBarController继承。 But I still see the same behaviour. 但是我仍然看到相同的行为。

I want to respond to my own question because I think maybe someone is facing the same situation, I have to say thanks to @Muneeba 我想回答我自己的问题,因为我认为也许有人正面临着同样的情况,我必须感谢@Muneeba

Well, first of all you have to know if your remote push notification needs to do a background fetch, this is important because if so the didReceiveRemoteNotification is called twice (first when you click on the notification alert, seconds when it opens the app), so you have to be aware of this. 好吧,首先,您必须知道您的远程推送通知是否需要进行后台获取,这很重要,因为如果这样,那么didReceiveRemoteNotification被调用两次(第一次单击通知警报,第二次打开应用程序),因此,您必须意识到这一点。

In my case I didn't do a background fetch, so my content-available:false flag was set to false. 就我而言,我没有进行后台获取,因此我的content-available:false标志设置为false。

Next thing to do is add you desire ["key":value] to the notification payload to know whether you want to open a specific view controller or not. 接下来要做的是将您想要的["key":value]到通知有效负载中,以了解您是否要打开特定的视图控制器。

Manage whether open a view controller or not. 管理是否打开视图控制器。

In my app I want to open a alert control pop up always showing the body of the push notification (with a OK button) and only when certain value is set open a alert control with a question to the user whether or not he wants to open the new content that arrives (normally this content is a web based content that open a webview embedded in a view controller) 在我的应用中,我想打开一个警报控件,该弹出窗口始终显示推送通知的主体(带有“确定”按钮),并且仅当设置了某些值时,才打开一个警报控件,询问用户是否要打开到达的新内容(通常,该内容是基于Web的内容,可以打开嵌入在视图控制器中的Webview)

In the didReceiveRemoteNotification : 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)
 }

Your RAMAnimatedTabBarController inherits which class? 您的RAMAnimatedTabBarController继承哪个类? if it inherits UIViewController / UITabbarController then take a UINavigationController and set its rootViewController to your RAMAnimatedTabBarController instance and then assign it as window's rootViewController and set the navigationBar hidden for this UINavigationController and when you want to push to specific Controller do like this 如果它继承了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