極光推送 根據服務器返回內容跳轉指定頁面

現在的很多應用當中會有消息推送的功能,本地推送就不多說了,大部分的時候都是遠程服務器推送,而蘋果自身的一套推送機制自己寫起來會比較繁瑣,大部分的時候會用到一些第三方的,比如 極光推送,友盟推送,百度推送等等,其實都大同小異.在公司項目中我集成的是極光推,就說說極光推送的那些事兒.

1. 集成. 在項目中集成極光推送,相對來說不復雜,文檔很詳盡,就不贅述 .稍微繁瑣的可能就是證書的配置,詳見 http://docs.jiguang.cn/client/ios_tutorials/.


2. 實際運用. 

        (1) 文檔集成完畢之後,可以用 極光的服務器 進行推送測試,測試時有全推和個推.全推就是針對服務器中所有用戶(在我們公司服務器中已註冊的用戶).個推就是針對於某一個用戶(可利用設置別名)來推送消息.一般是利用註冊用戶的 userID 來作爲標識在登陸的時候設置別名,在退出的時候清空別名.比如:

       /**alias  爲 UserID */

        [JPUSHService setTags:nil  alias:alias   callbackSelector:@selector(tagsAliasCallback:tags:alias:)                   target:self];

       此方法可在項目中任意地方調用.

     (2) 別名設置成功, 一切準備就緒之後,就會出現一個問題了,就是我在接收到服務器推送過來的消息之後是需要做一些事情的,比如點擊推送的消息跳轉到某一個指定的界面,或根據服務器返回不同消息的類型而跳轉到不同的界面,這裏肯定就得做一個判斷,該如何判斷呢?目前暫且我們的 app 已經是在啓動狀態.(app 啓動和沒啓動進入的方法會不同).
    

當我們接受到遠程服務器的消息的時候回調用方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

   

    if (application.applicationState == UIApplicationStateActive) { // 如果應用在前臺的話

         1. 提出自己想要的視圖..(自定義省略)     

         2. 點擊跳轉指定控制器(界面)  

           [self pushNotificationPageWithActive];

   }else{ // 應用在後臺

        [self pushNotificationPage:userInfo];

    }


    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

}

/** 應用如果在前臺運行的話推送的消息默認狀態下是沒有顯示的,所以需要在接收到遠程消息推送服務的時候做一個判斷,如果應用在前臺的話(項目變態需求需要有類似通知欄的展示)模擬了一個類似通知欄的效果,現在審覈中,目前還不知道這個會不會被拒哈*/

-(void)pushNotificationPage:(NSDictionary*)userInfo

{

    UITabBarController* tabBarVc=(UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController;

    if ([userInfo[@"type"] intValue] == 1) {  // 這裏就用數字簡寫了,規範點的可以自己寫成枚舉值

        tabBarVc.selectedIndex=3;

        YNavViewController* nav=tabBarVc.viewControllers[3];

        ChargeListController* newVc=[[ChargeListController alloc] init];

        [nav.viewControllers[0].navigationController pushViewController:newVc animated:YES];

    }else if([userInfo[@"type"] intValue] == 2){

        tabBarVc.selectedIndex=2;

        YNavViewController* nav=tabBarVc.viewControllers[2];

        NewsController* newVc=[[NewsController alloc] init];

        newVc.type=1;

        [nav.viewControllers[0].navigationController pushViewController:newVc animated:YES];

    }

}


另應用在前臺的話要做一個跳轉的話就得注意了:

1. 首先要獲得當前正在展示的控制器是否是根控制器,是否是根控制器這是有區別的./** 代碼可自行簡化哈*/

-(void)pushNotificationPageWithActive

{

    UIViewController *currentVC = [self getCurrentVC];

    if ([currentVC isKindOfClass:[UITabBarController class]]) {  //根控制器

        UITabBarController *rootVC = (UITabBarController *)currentVC;

        if ([self.type intValue] == 1) { //  充電通知

            YNavViewController *navc = rootVC.viewControllers[3];

            YMineTabViewController* mineVc=navc.viewControllers[0];

            ChargeListController* listVc=[[ChargeListController alloc] init];

            rootVC.selectedIndex=3;

            [mineVc.navigationController pushViewController:listVc animated:YES];


        }else if ([self.type intValue] == 2) // 最新資訊通知

        {

            YNavViewController *navc = rootVC.viewControllers[2];

            YComTableViewController* comVc = navc.viewControllers[0];

            NewsController *newVC = [[NewsController alloc]init];

            newVC.type=1;

            rootVC.selectedIndex = 2;

            [comVc.navigationController pushViewController:newVC animated:YES];

        }

    }else//非根控制器

        UITabBarController *rootVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;

        if ([self.type intValue] == 1) { //  充電通知

            YNavViewController *navc = rootVC.viewControllers[3];

            YMineTabViewController* mineVc=navc.viewControllers[0];

            [currentVC.navigationController popToRootViewControllerAnimated:NO];

            ChargeListController* listVc=[[ChargeListController alloc] init];

            rootVC.selectedIndex=3;

            [mineVc.navigationController pushViewController:listVc animated:YES];

            

        }else if ([self.type intValue] == 2) // 最新資訊通知

        {

            YNavViewController *navc = rootVC.viewControllers[2];

            YComTableViewController* comVc = navc.viewControllers[0];

            [currentVC.navigationController popToRootViewControllerAnimated:NO];

            rootVC.selectedIndex = 2;

            NewsController *newVC = [[NewsController alloc]init];

            newVC.type=1;

            [comVc.navigationController pushViewController:newVC animated:YES];

        }

    }

}


/**

 *  獲取當前屏幕顯示的viewcontroller

 */

- (UIViewController *)getCurrentVC

{

    UIViewController *result = nil;

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    if (window.windowLevel != UIWindowLevelNormal)

    {

        NSArray *windows = [[UIApplication sharedApplication] windows];

        for(UIWindow * tmpWin in windows)

        {

            if(tmpWin.windowLevel == UIWindowLevelNormal)

            {

                window = tmpWin;

                break;

            }

        }

    }

    UIView *frontView = [[window subviews] objectAtIndex:0];

    id nextResponder = [frontView nextResponder];

    if ([nextResponder isKindOfClass:[UIViewController class]]){

        result = nextResponder;

    }else{

        result = window.rootViewController;

    }

    return result;

}


做完以上這些,推送的基本的大部分功能應該都已經做完了哈,可以根據遠端服務器推送的不同類別推送全部或個人設備上點擊消息之後跳轉到到指定的頁面上面了.


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