iOS開發 極光推送收到通知後跳轉到指定頁面

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];//跳轉到指定頁面

    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章