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

时间:2013-12-16 10:35:17

标签: ios objective-c cocoa-touch push-notification tabbar

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

我已经将[self.tabBarController setSelectedIndex:2];放在viewDidAppear上,发生的事情是数据记录,它显示它通过tabBar 2 viewController,但它没有打开/显示该页面并保留在firstView控制器/第一个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。

3 个答案:

答案 0 :(得分:10)

您必须更改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;
 }
}

答案 1 :(得分: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)


            }}
        }}
}

答案 2 :(得分:0)

这是我的方法:

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let tabBarVC = storyboard.instantiateViewController(withIdentifier: StoryboardIDKeys.tabBarViewController.rawValue) as? TabBarViewController //{
        tabBarVC?.selectedIndex = 1
        self.window?.rootViewController = tabBarVC