user2767343 user2767343 - 3 months ago 9
iOS Question

Open specific tab bar when receive push notification

How can i open specific tab bar when receive push notification?

i already put

[self.tabBarController setSelectedIndex:2];
on viewDidAppear and what happen is in data logging, it showing that it passing through tabBar 2 viewController but it not open/showing that page and remain at the firstView controller/ first tabBar.

and then after i terminate the app and open back, automatically/ suddenly it open the tabBar 2 viewController.

can anyone give any idea or sample code to solve this?

this is in my 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 are located at tab bar 2..

Answer

You have to change tabbar selectedIndex like,

- (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;
 }
}
Comments