您的位置:首页 > 移动开发 > IOS开发

IOS 通知栏点击跳转指定页面

2016-11-11 14:36 591 查看
通过rootView跳转和实例化VC

//如果程序没有运行,点击通知栏推送消息跳转到指定页面。
if (launchOptions) {
        
        NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
        [pushJudge setObject:@"push" forKey:@"push"];
        [pushJudge synchronize];
        
        //NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
        [self presentViewControllerWithPushInfo];
    }

//如果程序正在运行时
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"Receive Notify: %@", [userInfo JSONString]);
    NSString *alertStr = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
//收到推送时程序正在前台运行,则给出一个alert,用户选择查看,跳转到指定页面
    if (application.applicationState == UIApplicationStateActive) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", alertStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看", nil];
        [alertView show];
    }
//收到推送时程序在后台运行,点击通知栏中的推送消息,跳转到指定页面
    if (application.applicationState != UIApplicationStateBackground && application.applicationState != UIApplicationStateActive) {
       
        [self presentViewControllerWithPushInfo];
    }
    
    [application setApplicationIconBadgeNumber:0];
    [BPush handleNotification:userInfo];
}

//收到推送时程序正在前台运行,则给出一个alert,用户选择查看,执行这个方法,并且跳转到指定页面
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
        [self presentViewControllerWithPushInfo];
    }
}

//点击推送消息跳转到指定页面的跳转方法
- (void)presentViewControllerWithPushInfo {
    
    NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
    [pushJudge setObject:@"push" forKey:@"push"];
    [pushJudge synchronize];

   //这个是我要通过推送跳转过去到页面
    MyOrderListViewController *orderListCtrl = [[MyOrderListViewController alloc] init];
    UINavigationController *pushNav = [[UINavigationController alloc] initWithRootViewController:orderListCtrl];
    [self.window.rootViewController presentViewController:pushNav animated:YES completion:nil];
}

注:
    NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
    [pushJudge setObject:@"push" forKey:@"push"];
    [pushJudge synchronize];
这个东西是一个辨别标示,在跳转到指定页面后拿到[pushJudge setObject:@"push" forKey:@"push"];从而可以知道是通过推送消息跳转过来的还是正常情况下通过导航栏导航过来的!通过这个“push”标示,如果是通过推送消息进入页面的,则用户可以在页面的(void)viewDidLoad这样写:
    NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
    if ([[pushJudge objectForKey:@"push"] isEqualToString:@"push"]) {
        
        [self.tabBarController.tabBar setHidden:YES];
        
      //给导航栏加一个返回按钮,便于将推送进入的页面返回出去,如果不是推送进入该页面,那肯定是通过导航栏进入的,则页面导航栏肯定会有导航栏自带的leftBarButtonItem返回上一个页面
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(rebackToRootViewAction)];
        self.navigationItem.leftBarButtonItem = leftButton;


- (void)rebackToRootViewAction {
    
//将标示条件置空,以防通过正常情况下导航栏进入该页面时无法返回上一级页面
    NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
    [pushJudge setObject:@"" forKey:@"push"];
    [pushJudge synchronize];
    
    [self dismissViewControllerAnimated:YES completion:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: