今天有朋友問我推送跳轉(zhuǎn)的問題殉挽,正好前段時間項目需求也用到了笨农,現(xiàn)在終結(jié)一下
主要有2種情況:
1.程序進(jìn)入后臺,但還沒有結(jié)速進(jìn)程吁脱,點擊通知進(jìn)到app.
2.程序已經(jīng)退出后臺,點擊通知進(jìn)到app.
先說第一種情況子漩,這種情況主要先獲取app當(dāng)前顯示頁豫喧,然后進(jìn)行跳轉(zhuǎn)石洗,直接上代碼
寫了一個demo幢泼,先看下我demo的結(jié)構(gòu)
建了2個分類,方便以后的調(diào)用
UIView+Tool.m 和 NSObject+Tool.m的實現(xiàn)
UIView+Tool.m
//通過響應(yīng)者鏈條獲取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的展現(xiàn)方式需要取到控制器的根視圖
- (UIViewController *)currentViewController
{
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// modal展現(xiàn)方式的底層視圖不同
// 取到第一層時讲衫,取到的是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中,新建一個按鈕涉兽,點擊按鈕創(chuàng)建一個本地通知
- (IBAction)btn:(id)sender {
[self pushMessage];
}
//創(chuàng)建本地通知
- (void)pushMessage
{
//10秒以后發(fā)送通知
NSDate *timeDate=[[NSDate alloc] initWithTimeIntervalSinceNow:10];
// 1.創(chuàng)建本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 2.設(shè)置本地通知的內(nèi)容
// 2.1.設(shè)置通知發(fā)出的時間
localNote.fireDate = timeDate;
// 2.2.設(shè)置通知的內(nèi)容
localNote.alertBody = @"push跳轉(zhuǎn)";
// 2.3.設(shè)置滑塊的文字(鎖屏狀態(tài)下:滑動來“解鎖”)
localNote.alertAction = @"解鎖";
// 2.4.決定alertAction是否生效
localNote.hasAction = NO;
// 2.7.設(shè)置有通知時的音效
localNote.soundName = @"buyao.wav";
// 2.8.設(shè)置額外信息
localNote.userInfo = @{@"type" : @"1"};
// 3.調(diào)用通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}
在AppDelegate 打開推送權(quán)限
- (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招驴,點擊通知跳轉(zhuǎn)到該類
// 本地通知回調(diào)函數(shù)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"noti:%@",notification);
//type為與后臺同事規(guī)定好的參數(shù),根據(jù)它來判斷跳轉(zhuǎn)到指定的頁面
if ([notification.userInfo[@"type"] isEqual:@"1"]) {
PushViewController *vc = [[PushViewController alloc] init];
[[self currentViewController].navigationController pushViewController:vc animated:YES];
}
}
第二種情況枷畏,當(dāng)程序已經(jīng)退出后臺别厘,直接上代碼
- (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 為遠(yuǎn)程推送
//UIApplicationLaunchOptionsLocalNotificationKey為本地推送 別忘記修改
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(@"localNotif= = %@", localNotif);
if (localNotif) {
//因為這個時候app還沒有初始化完成,我們不能獲取到當(dāng)前的顯示頁面拥诡。
有2種解決方案
1.跳轉(zhuǎn)延時幾秒后執(zhí)行触趴,預(yù)留一個初始化的時間
2.把推送的數(shù)據(jù)直接傳給ViewController氮发,直接在ViewController中做跳轉(zhuǎn),個人建議還是第2種
// if ([localNotif.userInfo[@"type"] isEqual:@"1"]) {
//第1種方案冗懦,延時執(zhí)行
// [self performSelector:@selector(pushComingViewController) withObject:nil afterDelay:2.0f];
// }
//第2種方案爽冕,傳參
//先在AppDelegate.h聲明屬性@property (strong, nonatomic) NSDictionary *userInfo;
//賦值
self.userInfo = localNotif.userInfo;
}
return YES;
}
//延遲跳轉(zhuǎn)
- (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上面了,有需要的可以下載