iOS在开放中,会使用到极光推送,然后收到推送时,往往需要跳转指定的界面,而跳转到指定界面时,又分为程序未杀死情况下的跳转和程序已杀死的跳转,即离线状况下的跳转:
当程序未杀死状况下的条状方法很简单:
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
//LXPMessageBoxViewController是要跳转到的目的页面
//LXPTabBarController是根视图
LXPMessageBoxViewController *ctl = [[LXPMessageBoxViewController alloc] init];
LXPTabBarController *tabBar = (LXPTabBarController *)self.window.rootViewController;//获取window的跟视图,并进行强制转换
if ([tabBar isKindOfClass:[UITabBarController class]]) {//判断是否是当前根视图
UINavigationController *nav = tabBar.selectedViewController;//获取到当前视图的导航视图
[nav.topViewController.navigationController pushViewController:ctl animated:YES];//获取当前跟视图push到的最高视图层,然后进行push到目的页面
}
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); // 系统要求执行这个方法
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
LXPMessageBoxViewController *ctl = [[LXPMessageBoxViewController alloc] init];
LXPTabBarController *tabBar = (LXPTabBarController *)self.window.rootViewController;
if ([tabBar isKindOfClass:[UITabBarController class]]) {
UINavigationController *nav = tabBar.selectedViewController;
[nav.topViewController.navigationController pushViewController:ctl animated:YES];
}
// Required,For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification:userInfo];
}
当程序杀死的情况下,又是另一种方法进行跳转到指定页面:
程序杀死时,进入程序肯定会走
AppDelegate的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法
那么我们首先在这个方法里面判断字典,是经过哪种形式进入的程序
如果是经过推送启动的程序,那么使用这个方法:([LXPAppContext context].notificationUserInfo是把启动返回的字典保存到本地,是一个字典接收)
[LXPAppContext context].notificationUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
也就是我们在这里获取到了是经过什么启动的程序,接下来,我们只需要在首页读取上面获取到的字典,如果字典不为空,则进行指定操作:
比如我们的首页是
#import "LXPBaseHomeViewController.h"
那我们就在这个视图出现时调用以下方法
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([LXPAppContext context].notificationUserInfo) {//如果是从推送通知唤醒
[LXPAppContext context].notificationUserInfo = nil;//进入这里后要把保存的字典重新设置为nil吧,不然那会不听的执行这个方法
LXPMessageBoxViewController *ctl = [[LXPMessageBoxViewController alloc] init];
[self.navigationController pushViewController:ctl animated:YES];//跳转到指定页面
}
}