1. 程式人生 > >iOS開發 極光推送收到通知後跳轉到指定頁面

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

iOS在開放中,會使用到極光推送,然後收到推送時,往往需要跳轉指定的介面,而跳轉到指定介面時,又分為程式未殺死情況下的跳轉和程式已殺死的跳轉,即離線狀況下的跳轉:

當程式未殺死狀況下的條狀方法很簡單:

// iOS 10 Support

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

// Required

//LXPMessageBoxViewController是要跳轉到的目的頁面

//LXPTabBarController是根檢視

LXPMessageBoxViewController *ctl = [[LXPMessageBoxViewControlleralloc] init];

LXPTabBarController *tabBar = (LXPTabBarController *)self.window.rootViewController;//獲取window的跟檢視,並進行強制轉換

if ([tabBar isKindOfClass:[UITabBarController

class]]) {//判斷是否是當前根檢視

UINavigationController *nav = tabBar.selectedViewController;//獲取到當前檢視的導航檢視

        [nav.topViewController.navigationControllerpushViewController:ctl animated:YES];//獲取當前跟檢視push到的最高檢視層,然後進行push到目的頁面

    }

NSDictionary * userInfo = response.notification.request.content.userInfo;

if

([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {

        [JPUSHServicehandleRemoteNotification:userInfo];

    }

    completionHandler();  // 系統要求執行這個方法

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

LXPMessageBoxViewController *ctl = [[LXPMessageBoxViewControlleralloc] init];

LXPTabBarController *tabBar = (LXPTabBarController *)self.window.rootViewController;

if ([tabBar isKindOfClass:[UITabBarControllerclass]]) {

UINavigationController *nav = tabBar.selectedViewController;

        [nav.topViewController.navigationControllerpushViewController:ctl animated:YES];

    }

// Required,For systems with less than or equal to iOS6

    [JPUSHServicehandleRemoteNotification:userInfo];

}

當程式殺死的情況下,又是另一種方法進行跳轉到指定頁面:

程式殺死時,進入程式肯定會走

AppDelegate的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法

那麼我們首先在這個方法裡面判斷字典,是經過哪種形式進入的程式

如果是經過推送啟動的程式,那麼使用這個方法:([LXPAppContextcontext].notificationUserInfo是把啟動返回的字典儲存到本地,是一個字典接收)

[LXPAppContextcontext].notificationUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

也就是我們在這裡獲取到了是經過什麼啟動的程式,接下來,我們只需要在首頁讀取上面獲取到的字典,如果字典不為空,則進行指定操作:

比如我們的首頁是

#import "LXPBaseHomeViewController.h"

那我們就在這個檢視出現時呼叫以下方法

- (void)viewDidAppear:(BOOL)animated

{

    [superviewDidAppear:animated];

if ([LXPAppContextcontext].notificationUserInfo) {//如果是從推送通知喚醒

        [LXPAppContextcontext].notificationUserInfo = nil;//進入這裡後要把儲存的字典重新設定為nil吧,不然那會不聽的執行這個方法

LXPMessageBoxViewController *ctl = [[LXPMessageBoxViewControlleralloc] init];

        [self.navigationControllerpushViewController:ctl animated:YES];//跳轉到指定頁面

    }

}