如何在appdelegate中推送导航?

时间:2017-06-16 05:04:23

标签: ios objective-c iphone

我已成功实施了推送通知,但如何从didReceiveRemoteNotification方法推送导航。

我的代码

Appdelegate代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    sleep(3);
    [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self registerForPushNotification:[UIApplication sharedApplication]];
    if (launchOptions != nil)
    {
        NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

        if (dictionary != nil)
        {
            NSLog(@"Launched from push notification: %@", dictionary);
            [self handlePush:application forRemoteNotification:dictionary];
        }
    }
 [self setupMainPage];
 [self.window makeKeyAndVisible];
    return YES;
}

- (void)setupMainPage
{
    if([[[NSUserDefaults standardUserDefaults] valueForKey:@"LoggedIn"] isEqualToString:@"0"])
    {
        if([[[NSUserDefaults standardUserDefaults] valueForKey:@"firstLaunch"] isEqualToString:@"0"])
        {
            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"firstLaunch"];

            IntroVC *introView = [[IntroVC alloc] initWithNibName:@"IntroVC" bundle:nil];
            self.navController = [[UINavigationController alloc] initWithRootViewController:introView];
            [self.navController setNavigationBarHidden:YES];
        }
        else
        {
            LoginVC *loginVC = [[LoginVC alloc] initWithNibName:@"LoginVC" bundle:[NSBundle mainBundle]];
            self.navController = [[UINavigationController alloc] initWithRootViewController:loginVC];
        }
    }
    else
    {
        HomeVC *homeView = [[HomeVC alloc] initWithNibName:@"HomeVC" bundle:nil];
        self.navController = [[UINavigationController alloc] initWithRootViewController:homeView];
    }
    self.window.rootViewController = self.navController;
}

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
        NSLog(@"%s",__FUNCTION__);
        NSLog(@"USER INFO - %@",userInfo);
        for (id key in userInfo)
        {
            NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
        }

        [self handlePush:application forRemoteNotification:userInfo];
    }





  -(void)handlePush:(UIApplication *)application forRemoteNotification:(NSDictionary *)userInfo
    {
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        NSLog(@"%@",userInfo);
        //NSString *pushType = [[[userInfo valueForKey:@"aps"] valueForKey:@"type"] lowercaseString];

        pushAlert = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        pushType = [[userInfo valueForKey:@"aps"] valueForKey:@"type"];
        NSInteger pushBadge = [[[userInfo valueForKey:@"aps"] valueForKey:@"badge"] integerValue];
        strReciverId = [CommonUtils getNotNullString:[[userInfo valueForKey:@"aps"] valueForKey:@"receiver_id"]];
        if (pushBadge > 0) {
            [application setApplicationIconBadgeNumber:pushBadge];
        }
        // aceept request
        if ([pushType isEqualToString:@"palrequest"])
        {
            [CommonUtils alertViewDelegateWithTitle:AlertTitle
                                        withMessage:pushAlert
                                          andTarget:self
                                    forCancelString:@"View Invitation"
                               forOtherButtonString:@"Cancel"
                                            withTag:101];
        }
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

    if (alertView.tag == 101)
    {
             MyMatchesVC *myMatchesVC = [[MyMatchesVC alloc] initWithNibName:@"MyMatchesVC" bundle:nil];
             myMatchesVC.strType = pushType;
             [navigationController pushViewController:myMatchesVC animated:YES];

    }

}

推动工作完美但我的问题是当我推动然后自动更改我的rootview。

我的应用流程

  1. 登录到应用,然后我更改了我的根视图(Homeview控制器)
  2. 现在我收到了其他用户的通知,然后我查看了邀请,然后点击我的比赛。
  3. 现在我从比赛中退回,然后更改为登录
  4. 需要此结果

    •   

      当我从mymatch回来然后弹出到主视图控制器

    请帮我解决这个问题。

5 个答案:

答案 0 :(得分:1)

试试这个!简单的解决方案:)

if (alertView.tag == 101)
{
         MyMatchesVC *myMatchesVC = [[MyMatchesVC alloc] initWithNibName:@"MyMatchesVC" bundle:nil];
         myMatchesVC.strType = pushType;
           [self.window.rootViewController showViewController:myMatchesVC sender:self];


}

//如果您已经是MyMatchVC。请按照以下风格。

  UINavigationController *snNavShareVC = (UINavigationController *)self.window.rootViewController;
    NSArray *viewControllers = snNavShareVC.viewControllers;
    UIViewController *findViewController = nil;
    for (UIViewController *vc in viewControllers) {
        NSLog(@"%@",[vc  class]);
        if ([MyMatchesVC class] == [vc class]) {
            findViewController = vc;
        }
    }
    if (findViewController) {
        [findViewController updateView:pushType]; // Write method to update view
    } else {
        MyMatchesVC *myMatchesVC = [[MyMatchesVC alloc] initWithNibName:@"MyMatchesVC" bundle:nil];
        myMatchesVC.strType = pushType;
        [self.window.rootViewController showViewController:myMatchesVC sender:self];

    }

//尝试此操作在导航控制器中添加堆栈视图控制器。

    ChatListVC *chatList = [[ChatListVC alloc]initWithNibName:@"ChatListVC" bundle:nil]; ChatDetailsVC *chatDetail = [[ChatDetailsVC alloc]initWithNibName:@"ChatDetailsVC" bundle:nil]; 
    chatDetail.strReceiverId = strReciverId; 
    NSMutableArray *controllers = [self.navController.viewControllers mutableCopy];
    [controllers addObject:chatList];
    [controllers addObject:chatDetail]; 
    [self.navigationController setViewControllers:controllers];

答案 1 :(得分:0)

请务必在致电String even = ""; String odd = ""; for (int n = sc.nextInt(); n != 0; n = sc.nextInt()) { if (n % 2 == 0) { even += even.isEmpty() ? Integer.toString(n) : ", " + n; } else { odd += odd.isEmpty() ? Integer.toString(n) : ", " + n; } } System.out.printf("Even: %s%nOdd: %s%n", even.toString(), odd.toString());

中的[self setupMainPage];之前致电[self handlePush:
didFinishLaunchingWithOptions:

更新alertView - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. sleep(3); [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self setupMainPage]; [self registerForPushNotification:[UIApplication sharedApplication]]; if (launchOptions != nil) { NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (dictionary != nil) { NSLog(@"Launched from push notification: %@", dictionary); [self handlePush:application forRemoteNotification:dictionary]; } } [self.window makeKeyAndVisible]; return YES; } 并使用clickedButtonAtIndex代替self.navController

(UINavigationController *)self.window.rootViewController

答案 2 :(得分:0)

你必须保持登录或注销状态,你可以判断你必须弹出的位置,如果你要注销,那么你必须popToRootViewControllerAnimated,如果你是登录,那么你必须popToViewController: animated:你的主页

UINavigationController *navController = (UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
if (isLogin) {
    [navController popToRootViewControllerAnimated:NO];
}
else{
    NSArray *arrView = [navController childViewControllers];
    for (id vc in arrView) {
        if ([vc  isKindOfClass: [Homeview class]]) {
            [navController popToViewController:vc animated:NO];
            break;
        }
}

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"VC"];
[navController pushViewController:vc animated:YES];

答案 3 :(得分:0)

如果您想按照要求执行操作,当您从MyMatchesVC返回然后弹出到HomeVC控制器时,您需要从MyMatchesVC推出HomeVC否则MyMatchesVC直接来自根导航控制器,其中LoginVC为根视图控制器,因此您不会将HomeVC作为Root。为此你必须像下面那样。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

        if (alertView.tag == 101)
        {
                 HomeVC *homeView = [[HomeVC alloc] initWithNibName:@"HomeVC" bundle:nil];
                 navigationController = [[UINavigationController alloc] initWithRootViewController:introView];

                 MyMatchesVC *myMatchesVC = [[MyMatchesVC alloc] initWithNibName:@"MyMatchesVC" bundle:nil];
                 myMatchesVC.strType = pushType;
                 [navigationController pushViewController:myMatchesVC animated:YES];

        }

    }

答案 4 :(得分:0)

MyMatchesVC *myMatchesVC = [[MyMatchesVC alloc] initWithNibName:@"MyMatchesVC" bundle:nil];
myMatchesVC.strType = pushType;
[((UINavigationController *)self.window.rootViewController) setViewControllers:@[((UINavigationController *)self.window.rootViewController).visibleViewController, myMatchesVC] animated:YES];

转到可见的viewcontroller。

MyMatchesVC *myMatchesVC = [[MyMatchesVC alloc] initWithNibName:@"MyMatchesVC" bundle:nil];
HomeVC *home = [[HomeVC alloc] initWithNibName:@"HomeVC" bundle:nil];

myMatchesVC.strType = pushType;
[((UINavigationController *)self.window.rootViewController) setViewControllers:@[home, myMatchesVC] animated:YES];

通过编辑HomeVC到您家中来转到您的家庭视图控制器