iOS 点击推送跳转详情页

系统 API :iOS < 77 <= iOS < 10iOS >= 10
app 状态:后台运行前台运行未启动

iOS 6 及以前接收到远程推送的代理

- application: didReceiveRemoteNotification;

- application: didReceiveRemoteNotification 官方文档
If the app is running, the app calls this method to process incoming remote notifications. If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification.
Instead, your implementation of the application:willFinishLaunchingWithOptions:
or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.

如果应用程序正在运行,应用程序调用这个方法来处理传入的远程通知。
如果应用程序在远程通知到达时没有运行,该方法会启动应用程序,并提供启动信息。应用程序不会调用这个方法来处理远程通知。这时你可以通过application:didFinishLaunchingWithOptions:方法来获得推送信息。
虽然是iOS 6之前的,但使用范围是NS_DEPRECATED_IOS(3_0, 10_0)。只是iOS 7之后有了更好的选择,所以这个就显得有点鸡肋了。如果要适配iOS 6及以前还需实现这个

iOS 7(包含) 到 iOS 10(不包含)新增代理。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"[XGDemo] receive slient Notification");
    NSLog(@"[XGDemo] userinfo %@", userInfo);
    if (application.applicationState == UIApplicationStateActive) {
        // 前台处理
    }else {
        [self pushToVCWithNotificationInfo:userInfo];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

- application:didReceiveRemoteNotification:fetchCompletionHandler 官方文档
 Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification:method, which is called only when your app is running in the foreground,the system calls this method when your app is running in the foreground or background.
 If your delegate implements both methods, the app object calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method.

application:didReceiveRemoteNotification:只有当应用程序在前台运行调用,而这个方法在前台、后台、未启动都能调用。如果两个代理方法都被实现了,系统将只调用application:didReceiveRemoteNotification:fetchCompletionHandler:

因为我们应用只适配iOS 8及以上,所以我就没有再实现- (void)application: didReceiveRemoteNotification ;毕竟有了更新的代理,更简单不用单独处理应用未启动的情况。当然也可以使用- application: didReceiveRemoteNotification;

iOS 10及以上新增代理

首先要在application:didFinishLaunchingWithOptions中加入

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
#endif

然后实现代理

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
    NSLog(@"[XGDemo] click notification");
    //收到推送的请求
    UNNotificationRequest *request = response.notification.request;
    //收到推送的内容
    UNNotificationContent *content = request.content;
    //收到用户的基本信息
    NSDictionary *userInfo = content.userInfo;
    [self pushToVCWithNotificationInfo:userInfo];
    completionHandler();
}
- userNotificationCenter: willPresentNotification: withCompletionHandler;   // App 在前台弹通知需要调用这个接口

跳转详情页

- (void)jumpWithNotificationInfo:(NSDictionary *)info {
    // 如果为空就返回
    if (!info) {
        return;
    }
    // 获取包含的信息
    NSString *type = info[@"type"];
    // 首先,到最外层
    UIViewController *topView;
    topView = [self topViewController]; // 当前控制器
    [topView.navigationController popToRootViewControllerAnimated:NO]; // 跳到一级界面
    if ([type isEqualToString:@"commentpass"]) { // 足迹
        self.mainVC.selectedIndex = 0;  // 然后,选中第几个模块
        DiscoveryDetialViewController *VC = [[DiscoveryDetialViewController alloc] init];
        UINavigationController *nav = self.mainVC.childViewControllers[0];
        VC.hidesBottomBarWhenPushed = YES;
        [nav pushViewController:VC animated:YES];
    }
}

你可能感兴趣的:(iOS 点击推送跳转详情页)