根据推送参数实现跳转

项目结构:UITabBarController->UINavigationController->UIViewController
实现结果:根据推送参数跳转对应的页面

首先先分析一番:
我们会在AppDelegate里收到推送的参数,一般是个JSON,我们可以在AppDelegate定义一个字典来接受这个JSON,然后需要根据这个字典来进行跳转。我们可以定义一个类来处理跳转,在这个类里,先获取到用户点到的tabBarItem,获取这个tabBarItem对应的UINavigationController,然后使用导航器进行跳转。

接下来开始写代码:
在AppDelegate定义一个字典接受推送的参数:

@interface AppDelegate : UIResponder 
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSDictionary *pushDic;//推送参数
@end

在收到推送的方法接受推送来的参数
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
_pushDic = userInfo;
}

参数是这样:

_pushDic = @{@"type":@"1",
              @"text":@"miss",
              @"url":@""};

type:跳转的页面,1.普通页面 2.H5 将会打开一个链接
text:当type等于1时需要
url:    当type等于2时需要

新建一个ReceivePush类,来处理跳转:
.h
#import
@interface MSReceivePush : NSObject
+ (void)reveivePush;
@end

.m
#import "MSReceivePush.h"
#import "AppDelegate.h"
#import "PushOneViewController.h"
#import "PushTwoViewController.h"
@implementation MSReceivePush

+ (void)reveivePush{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (app.pushDic.count == 0)return;

    //获取TabBarController
    UITabBarController *tabVC = (UITabBarController *)[[[UIApplication sharedApplication] delegate] window].rootViewController;
    //获取用户选择的tabBarItem的NavigationController
    UINavigationController *nav = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //根据参数跳转页面
    if ([app.pushDic[@"type"] integerValue] == 1) {
        PushOneViewController *one = [mainStoryboard instantiateViewControllerWithIdentifier:@"pushOne"];
        one.pushString = app.pushDic[@"text"];
        one.hidesBottomBarWhenPushed = YES;
        [nav pushViewController:one animated:YES];
    }else if([app.pushDic[@"type"] integerValue] == 2){
        PushTwoViewController *two = [mainStoryboard instantiateViewControllerWithIdentifier:@"pushTwo"];
        two.url = app.pushDic[@"url"];
        two.hidesBottomBarWhenPushed = YES;
        [nav pushViewController:two animated:YES];
    }

}

@end

然后我们在需要跳转的时候调用方法就可以了

 [MSReceivePush reveivePush];

Git:根据模拟推送参数进行跳转

你可能感兴趣的:(根据推送参数实现跳转)