Open specific tab bar when receive push notification

♀尐吖头ヾ 提交于 2020-01-14 14:31:34

问题


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.


回答1:


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



回答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)


            }}
        }}
}



回答3:


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


来源:https://stackoverflow.com/questions/20608696/open-specific-tab-bar-when-receive-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!