iOS 點(diǎn)擊推送信息跳轉(zhuǎn)到指定頁(yè)面 + 獲取當(dāng)前顯示頁(yè)

今天有朋友問(wèn)我推送跳轉(zhuǎn)的問(wèn)題,正好前段時(shí)間項(xiàng)目需求也用到了,現(xiàn)在終結(jié)一下
主要有2種情況:
1.程序進(jìn)入后臺(tái),但還沒(méi)有結(jié)速進(jìn)程,點(diǎn)擊通知進(jìn)到app.
2.程序已經(jīng)退出后臺(tái),點(diǎn)擊通知進(jìn)到app.

先說(shuō)第一種情況,這種情況主要先獲取app當(dāng)前顯示頁(yè),然后進(jìn)行跳轉(zhuǎn),直接上代碼
寫(xiě)了一個(gè)demo,先看下我demo的結(jié)構(gòu)


8A03E4E3-D4FF-4455-BB68-19DDCD33D307.png

建了2個(gè)分類(lèi),方便以后的調(diào)用


![B4852E6A-CACC-4B20-B4C7-4A7530A21A11.png](http://upload-images.jianshu.io/upload_images/683626-7d2203f54a43adba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

UIView+Tool.m 和 NSObject+Tool.m的實(shí)現(xiàn)

  UIView+Tool.m
  //通過(guò)響應(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"
//通過(guò)控制器的布局視圖可以獲取到控制器實(shí)例對(duì)象    modal的展現(xiàn)方式需要取到控制器的根視圖
- (UIViewController *)currentViewController
{
 UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// modal展現(xiàn)方式的底層視圖不同
// 取到第一層時(shí),取到的是UITransitionView,通過(guò)這個(gè)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中,新建一個(gè)按鈕,點(diǎn)擊按鈕創(chuàng)建一個(gè)本地通知

- (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ā)出的時(shí)間
localNote.fireDate = timeDate;
// 2.2.設(shè)置通知的內(nèi)容
localNote.alertBody = @"push跳轉(zhuǎn)";
// 2.3.設(shè)置滑塊的文字(鎖屏狀態(tài)下:滑動(dòng)來(lái)“解鎖”)
localNote.alertAction = @"解鎖";
// 2.4.決定alertAction是否生效
localNote.hasAction = NO;
// 2.7.設(shè)置有通知時(shí)的音效
localNote.soundName = @"buyao.wav";

// 2.8.設(shè)置額外信息
localNote.userInfo = @{@"type" : @"1"};
// 3.調(diào)用通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

在AppDelegate 打開(kāi)推送權(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,點(diǎn)擊通知跳轉(zhuǎn)到該類(lèi)

// 本地通知回調(diào)函數(shù)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"noti:%@",notification);
//type為與后臺(tái)同事規(guī)定好的參數(shù),根據(jù)它來(lái)判斷跳轉(zhuǎn)到指定的頁(yè)面
if ([notification.userInfo[@"type"] isEqual:@"1"]) {
    PushViewController *vc = [[PushViewController alloc] init];
    [[self currentViewController].navigationController pushViewController:vc animated:YES];
    }
}

第二種情況,當(dāng)程序已經(jīng)退出后臺(tái),直接上代碼

- (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) {
//因?yàn)檫@個(gè)時(shí)候app還沒(méi)有初始化完成,我們不能獲取到當(dāng)前的顯示頁(yè)面。
 有2種解決方案
1.跳轉(zhuǎn)延時(shí)幾秒后執(zhí)行,預(yù)留一個(gè)初始化的時(shí)間
2.把推送的數(shù)據(jù)直接傳給ViewController,直接在ViewController中做跳轉(zhuǎn),個(gè)人建議還是第2種

 //        if ([localNotif.userInfo[@"type"] isEqual:@"1"]) {
              //第1種方案,延時(shí)執(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上面了,有需要的可以下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容