我们经常碰到这样的需求,从根据推送通知内容,跳转到指定的页面。下面主要写一下要点,至于如何配置极光推送,请参考官方文档,这里不赘述了。
//在appDelegate的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//如果程序没有运行,点击通知栏推送消息跳转到指定页面。
if (launchOptions) {
用NSUserDefaults来保存一个标识,用来进行判断是否是从推送进入的页面
NSUserDefaults *pushJudgeID = [NSUserDefaults standardUserDefaults];
[pushJudgeID setObject:@"push" forKey:@"push"];
[pushJudgeID synchronize];
[self presentViewControllerWithPushInfo];//方法在下面
}
//极光推送iOS7以上版本收到推送通知调用的方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
self.pushDic = userInfo;//这里是声明了一个字典,因为要在下面的presentViewControllerWithPushInfo方法中,需要用到这个字典来进行判断
//收到推送时程序在后台运行,点击通知栏中的推送消息,跳转到指定页面
//判断当前程序的状态
if (application.applicationState != UIApplicationStateBackground && application.applicationState != UIApplicationStateActive) {
[self presentViewControllerWithPushInfo];
}
[application setApplicationIconBadgeNumber:0];
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
-(void)presentViewControllerWithPushInfo{
NSUserDefaults *pushJudgeID = [NSUserDefaults standardUserDefaults];
[pushJudgeID setObject:@"push" forKey:@"push"];
[pushJudgeID synchronize];
if ([[self.pushDic objectForKey:@"type"] isEqualToString:@"1"]) {
//这个是我要通过推送跳转过去到页面
GiftCenterViewController *giftVC = [[GiftCenterViewController alloc] init];
UINavigationController *pushNav = [[UINavigationController alloc] initWithRootViewController:giftVC];
[self.window.rootViewController presentViewController:pushNav animated:YES completion:nil];
}
}
//NSUserDefaults *pushJudgeID = [NSUserDefaults standardUserDefaults];
//[pushJudgeID setObject:@"push" forKey:@"push"];
//[pushJudgeID synchronize];
//根据上面"push"这个标识,可以判断出来是从推送通知进来的,所以可以在需要想要进入的控制器的viewDidLoad方法中进行判断
NSUserDefaults *pushJudgeID = [NSUserDefaults standardUserDefaults];
if ([[pushJudgeID objectForKey:@"push"] isEqualToString:@"push"]) {
//给导航栏加一个返回按钮,便于将推送进入的页面返回出去,如果不是推送进入该页面,那肯定是通过导航栏进入的,则页面导航栏肯定会有导航栏自带的leftBarButtonItem返回上一个页面
-(void)viewDidLoad{
//创建返回按钮
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];
}