iOS 点击推送信息跳转到指定页面 + 获取当前显示页
2016-08-19 本文已影响2392人
枯流水
今天有朋友问我推送跳转的问题,正好前段时间项目需求也用到了,现在终结一下
主要有2种情况:
1.程序进入后台,但还没有结速进程,点击通知进到app.
2.程序已经退出后台,点击通知进到app.
先说第一种情况,这种情况主要先获取app当前显示页,然后进行跳转,直接上代码
写了一个demo,先看下我demo的结构
8A03E4E3-D4FF-4455-BB68-19DDCD33D307.png
建了2个分类,方便以后的调用
![B4852E6A-CACC-4B20-B4C7-4A7530A21A11.png](http:https://img.haomeiwen.com/i683626/7d2203f54a43adba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
UIView+Tool.m 和 NSObject+Tool.m的实现
UIView+Tool.m
//通过响应者链条获取view所在的控制器
- (UIViewController *)parentController
{
UIResponder *responder = [self nextResponder];
while (responder) {
i f ([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)responder;
}
responder = [responder nextResponder];
}
return nil;
}
NSObject+Tool.m 引入#import "UIView+Tool.h"
//通过控制器的布局视图可以获取到控制器实例对象 modal的展现方式需要取到控制器的根视图
- (UIViewController *)currentViewController
{
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// modal展现方式的底层视图不同
// 取到第一层时,取到的是UITransitionView,通过这个view拿不到控制器
UIView *firstView = [keyWindow.subviews firstObject];
UIView *secondView = [firstView.subviews firstObject];
UIViewController *vc = [secondView parentController];
if ([vc isKindOfClass:[UITabBarController class]]) {
UITabBarController *tab = (UITabBarController *)vc;
if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
return [nav.viewControllers lastObject];
} else {
return tab.selectedViewController;
}
} else if ([vc isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)vc;
return [nav.viewControllers lastObject];
} else {
return vc;
}
return nil;
}
在ViewController中,新建一个按钮,点击按钮创建一个本地通知
- (IBAction)btn:(id)sender {
[self pushMessage];
}
//创建本地通知
- (void)pushMessage
{
//10秒以后发送通知
NSDate *timeDate=[[NSDate alloc] initWithTimeIntervalSinceNow:10];
// 1.创建本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 2.设置本地通知的内容
// 2.1.设置通知发出的时间
localNote.fireDate = timeDate;
// 2.2.设置通知的内容
localNote.alertBody = @"push跳转";
// 2.3.设置滑块的文字(锁屏状态下:滑动来“解锁”)
localNote.alertAction = @"解锁";
// 2.4.决定alertAction是否生效
localNote.hasAction = NO;
// 2.7.设置有通知时的音效
localNote.soundName = @"buyao.wav";
// 2.8.设置额外信息
localNote.userInfo = @{@"type" : @"1"};
// 3.调用通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}
在AppDelegate 打开推送权限
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
return YES;
}
新建PushViewController,点击通知跳转到该类
// 本地通知回调函数
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"noti:%@",notification);
//type为与后台同事规定好的参数,根据它来判断跳转到指定的页面
if ([notification.userInfo[@"type"] isEqual:@"1"]) {
PushViewController *vc = [[PushViewController alloc] init];
[[self currentViewController].navigationController pushViewController:vc animated:YES];
}
}
第二种情况,当程序已经退出后台,直接上代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
//这里要注意下 UIApplicationLaunchOptionsRemoteNotificationKey 为远程推送
//UIApplicationLaunchOptionsLocalNotificationKey为本地推送 别忘记修改
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(@"localNotif= = %@", localNotif);
if (localNotif) {
//因为这个时候app还没有初始化完成,我们不能获取到当前的显示页面。
有2种解决方案
1.跳转延时几秒后执行,预留一个初始化的时间
2.把推送的数据直接传给ViewController,直接在ViewController中做跳转,个人建议还是第2种
// if ([localNotif.userInfo[@"type"] isEqual:@"1"]) {
//第1种方案,延时执行
// [self performSelector:@selector(pushComingViewController) withObject:nil afterDelay:2.0f];
// }
//第2种方案,传参
//先在AppDelegate.h声明属性@property (strong, nonatomic) NSDictionary *userInfo;
//赋值
self.userInfo = localNotif.userInfo;
}
return YES;
}
//延迟跳转
- (void)pushComingViewController{
PushViewController *vc = [[PushViewController alloc] init];
[[self currentViewController].navigationController pushViewController:vc animated:YES];
}
在ViewController中
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
if ([app.userInfo[@"type"] isEqual:@"1"]) {
PushViewController *vc = [[PushViewController alloc] init];
[[self currentViewController].navigationController pushViewController:vc animated:YES];
}
}
参考http://www.knowsky.com/883628.html
把demo放到github上面了,有需要的可以下载