收到推送通知时打开特定的标签栏

接收推送通知时,如何打开特定的标签栏?

我已经把[self.tabBarController setSelectedIndex:2]; 在viewDidAppear和数据logging中发生了什么,它显示它通过tabBar 2 viewController,但它不打开/显示该页面,并保持在第一个控制器/第一个tabBar。

然后我终止应用程序并打开回来,自动/突然它打开tabBar 2 viewController。

任何人都可以给任何想法或示例代码来解决这个问题?

这是在我的didReceiveRemoteNotification;

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"Remote notification received"); if([userInfo valueForKey:@"app"]) { NSString *action_app = [userInfo valueForKey:@"app"]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:action_app forKey:@"app"]; NewVC *sample=[[NewVC alloc]init]; [sample viewDidLoad]; //[self.tabBarController setSelectedIndex:2]; [self clearNotifications]; }else{ NSLog(@"---nothing to read---"); } } 

NewVC位于标签栏2 ..

你必须改变tabbar selectedIndex像,

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { if([userInfo valueForKey:@"app"]) { NSString *action_app = [userInfo valueForKey:@"app"]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:action_app forKey:@"app"]; UITabBarController *tabb = (UITabBarController *)self.window.rootViewController; tabb.selectedIndex = 2; } } 

对于Swift

这是推送通知数据:

 { "aps": { "alert" : "Hello Your Order ID 107922341457c134962d002 is Ready. Thank you For choosing us!!", "badge" : "1", "sound" :"default" }, "order": { "item_name" : "Spanish Masala", "order_id":"107922341457c134962d002" , "food_id":"15" } } 

这是 AppDelegate.swift 的代码

  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { if loginFlag{ if let temp = userInfo["order"] as? Dictionary<String, AnyObject> { sharedObject.orderDetailsFromPushNotification = temp if let navController = self.window?.rootViewController as? UINavigationController{ let storyboard = UIStoryboard(name: "Main", bundle: nil) if let myViewController = storyboard.instantiateViewControllerWithIdentifier("tabHome") as? MyTabViewController{ myViewController.selectedIndex = 1 navController.pushViewController(myViewController, animated: true) }} }} }