iOS远程推送点击消息跳转到指定页面

iOS开发中经常会用到远程推送,点击推送的消息进入指定的页面更是一项基本功能,本篇文章将介绍一下如何点击消息进入指定页面。
iOS点击消息进入app将分三种情况:app在后台运行,app在前台运行,app未启动。不同情况会走不同的方法,其中app在后台运行和前台运行会走如下方法:
其中,iOS7之前会走如下方法:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    // App 收到推送的通知
    NSLog(@"********** ios7.0之前 **********");
    // 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
    if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
        NSLog(@"acitve or background");
        UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"提示" message:userInfo[@"aps"][@"alert"][@"body"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即查看", nil];
        [alertView show];
    }
    else//杀死状态下,直接跳转到跳转页面。
    {
        TakeReceiptsListViewController *VC = [TakeReceiptsListViewController new];
        VC.enterStyle = NO;
        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:VC];
        [self.window.rootViewController presentViewController:na animated:YES completion:nil];
    }
}

iOS7之后会走下面的方法:

#pragma mark - 此方法是 用户点击了通知,应用在前台 或者开启后台并且应用在后台 时调起
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    completionHandler(UIBackgroundFetchResultNewData);
    // 打印到日志 textView 中
    NSLog(@"********** iOS7.0之后 background **********");
    // 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
    if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
        NSLog(@"acitve or background");
                UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"提示" message:userInfo[@"aps"][@"alert"][@"body"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即查看", nil];
                [alertView show];
    }
    else//杀死状态下,直接跳转到跳转页面。
    {
        TakeReceiptsListViewController *VC = [TakeReceiptsListViewController new];
        VC.enterStyle = NO;
        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:VC];
        [self.window.rootViewController presentViewController:na animated:YES completion:nil];
    }
}

iOS10 Apple更新了推送方法,收到通知点击触发的方法- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler已被弃用,使用新的方法如下:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
    completionHandler(UIBackgroundFetchResultNewData);
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    [self pushToNewsDetailViewController:userInfo];
}

如果app未运行的话会先走走如下方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

然后再走:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

你可能感兴趣的:(iOS远程推送点击消息跳转到指定页面)