neteot
neteot

Reputation: 933

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. 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:

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. But I still see the same behaviour.

Upvotes: 1

Views: 7910

Answers (2)

neteot
neteot

Reputation: 933

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

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.

In my case I didn't do a background fetch, so my content-available:false flag was set to 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.

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)

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

Upvotes: 4

Muneeba
Muneeba

Reputation: 1776

Your RAMAnimatedTabBarController inherits which class? 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

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

Upvotes: 0

Related Questions