iOS 遠程推送(極光推送) 根據後臺推送內容的不同跳轉指定頁面(不斷更新)

基本步驟就不再說了,可以谷歌
遠程推送應用配置過程
一. 創建支持遠程推送功能的App ID
二. 創建推送證書(開發證書和發佈證書)和描述文件
三. 下載CER文件,並導入鑰匙串管理
四. 我們需要重新生成一下配置文件
下面開始就介紹,點擊推送的內容跳轉指定頁面
 現在點擊推送消息,有兩種跳轉方式:
one.打開應用,跳轉到應用首頁;
默認的效果是點擊推送消息,會直接進入應用,什麼都不用設置,只要註冊極光應用就可以。可以參考官方文檔,寫的非常詳細,直接拷貝文檔代碼即可。
two.打開應用,跳轉到指定頁面。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

    if (application.applicationState.applicationState == UIApplicationStateActive) {
        //程序運行時收到通知,先彈出消息框
        NSLog(@"程序在前臺");
        [self popAlert:userInfo];


    }

    else{
        //程序已經關閉或者在後臺運行
        [self pushToViewControllerWhenClickPushMessageWith:userInfo];
        //這裏也可以發送個通知,跳轉到指定頁面
        // [self readNotificationVcWithUserInfo:userInfo];

    }

    [application setApplicationIconBadgeNumber:0];

    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);
}
---------------
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification 
       withCompletionHandler:(void (^)(NSInteger))completionHandler {

    NSDictionary * userInfo = notification.request.content.userInfo;
    UNNotificationRequest *request = notification.request; // 收到推送的請求
    UNNotificationContent *content = request.content; // 收到推送的消息內容


    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];


        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            //程序運行時收到通知,先彈出消息框
            NSLog(@"程序在前臺");
            [self popAlert:userInfo];

        }

        else{
            //跳轉到指定頁面
            [self pushToViewControllerWhenClickPushMessageWith:userInfo];
            //這裏也可以發送個通知,跳轉到指定頁面
            // [self readNotificationVcWithUserInfo:userInfo];

        }

    }
    // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設置
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}


-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void (^)())completionHandler {
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    [[NSUserDefaults standardUserDefaults] setObject:@"Inactive" forKey:@"applicationState"];

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];


        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            //程序運行時收到通知,先彈出消息框
            [self popAlert:userInfo];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationState" object:@"0"];

        }

        else{

            [self pushToViewControllerWhenClickPushMessageWith:userInfo];
            //這裏也可以發送個通知,跳轉到指定頁面
            // [self readNotificationVcWithUserInfo:userInfo];
        }

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

如果在程序運行時收到通知,這時消息欄不會顯示通知,所以如果想讓用戶收到通知的話,應該是給用戶一個彈框提醒,告訴用戶有消息通知,當用戶點擊提示框中的確認查看按鈕時,跳轉到指定的頁面
#pragma mark -- 程序運行時收到通知
-(void)popAlert:(NSDictionary *)pushMessageDic{

        UIAlertController *alertController = 
        [UIAlertController alertControllerWithTitle:@"" message:[[pushMessageDic objectForKey:@"aps"]objectForKey:@"alert"]
                                                                          preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *confirmAction = 
        [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [self pushToViewControllerWhenClickPushMessageWith:pushMessageDic];
                                                              }];

        UIAlertAction *cancelAction = 
        [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

                                                               }];

        [alertController addAction:confirmAction];
        [alertController addAction:cancelAction];
        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];


}

跳轉到指定頁面的方法。跳轉到指定頁面的話,可能會需要某些參數,這時可以根後臺商定,根據參數跳轉到相應的頁面,同時也讓後臺把你需要的參數返回來。比如我和後臺商定根據“type”這個參數確定跳轉的頁面。如果是跳轉到次級頁面,這裏重要的是要找到正確的viewcontroller,用controller的nav進行push新頁面。比如我的MessageViewController是用tabar的第一個item中的FirstViewController的nav進行push出來的,那麼,當我點擊通知消息想到跳轉到MessageViewController,只要找到FirstViewController就可以了。

-(void)pushToViewControllerWhenClickPushMessageWith:(NSDictionary*)msgDic{


    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];

            if ([[msgDic objectForKey:@"type"] integerValue]==0){

//      跳轉到第一個tabbarItem,這時直接設置 UITabBarController的selectedIndex屬性就可以

             self.tabController.selectedIndex = 0;

            }else if ([[msgDic objectForKey:@"type"] integerValue]==1){

                //跳轉到第二個tabbarItem

                self.tabController.selectedIndex = 1;


            }else if ([[msgDic objectForKey:@"type"] integerValue]==2){
                //跳轉到第三個tabbarItem


                self.tabController.selectedIndex = 2;

            }else if ([[msgDic objectForKey:@"type"] integerValue]==3){
                //詳情,這是從跳轉到第一個tabbarItem跳轉過去的,所以我們可以先讓tabController.selectedIndex =0;然後找到VC的nav。

                self.tabController.selectedIndex =0;
                MessageViewController * VC = [[MessageViewController alloc]init];
                [VC setHidesBottomBarWhenPushed:YES];
            //因爲我用了三方全屏側滑手勢,所以要找的是第一個tabbarController中的viewController的JTNavigationController ,接着再找JTNavigationController 裏面的jt_viewControllers.lastObject,這樣就可以找到FirstViewController了,然後跳轉的時候就和正常的跳轉一樣了
                JTNavigationController *nav=(JTNavigationController *)self.tabController.viewControllers[0];
                UIViewController *vc=(UIViewController*)nav.jt_viewControllers.lastObject;

                [vc.navigationController pushViewController:VC animated:NO];

            }else {

            }

//也可以發送通知跳轉

- (void)readNotificationVcWithUserInfo:(NSDictionary *)userInfo {

    

    MessageViewController *vc = [[MessageViewController alloc] init];

    challengeVc.userInfo = userInfo;

    

    BaseNavigationController *challengeNaviVc = [[BaseNavigationController alloc] initWithRootViewController:challengeVc];

    

    [self.window.rootViewController presentViewController:vc animated:YES completion:nil];

}








發佈了45 篇原創文章 · 獲贊 9 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章