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 tabbar 2.
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;
}
}
For Swift
This is the Push notification data:
{
"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"
}
}
Here is the code in 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)
}}
}}
}
Here how I do it:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarVC = storyboard.instantiateViewController(withIdentifier: StoryboardIDKeys.tabBarViewController.rawValue) as? TabBarViewController //{
tabBarVC?.selectedIndex = 1
self.window?.rootViewController = tabBarVC