1. 程式人生 > >點擊極光推送,實現跳轉

點擊極光推送,實現跳轉

定義 ctf 每次 con 消息 center 不同的 tno handler

  說實話,極光推送接觸過好幾遍了,但是每次開發都是實現簡單的展示功能,最近接手的一款app要求只在後臺展示,還要實現點擊通知欄跳轉到相應的詳情界面,於是便以為很簡單的開始了,而且還很嗨的那種,以為自己沒問題了(當時自己用的iOS10以上的iPhone)。但是最後測試階段,出現各種問題,各種調試都不行,簡單總結一下當時出現的問題(不過前提條件是已經按照極光官方文檔的要求集成好了,而且用極光的控制臺發送消息通知一切都正常)

1)不同系統的手機,app在後臺通知的展示和點擊會走不同的代理方法

比如說iOS8系統下,展示通知和點擊通知都會走這個方法

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


fetchCompletionHandler:(void
(^)(UIBackgroundFetchResult))completionHandler

又比如說iOS10系統下,展示通知居然會走上面的方法,還會在點擊通知的時候走下面這個方法

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

2)前臺和後臺,也會影響調用不同的代理方法

3)你以為這是最後一個問題,那你就錯了,這是都沒法繼續總結了,總之就是出現毫無規律可言的亂

相信看到這裏,很多人還是不理解,這裏盡量放心,是可以直接忽略跳過前面的。要想實現點擊極光推送去跳轉詳情,主要看下面的這張圖,重點來了!重點來了!重點來了!

  技術分享圖片

  上面這張圖是我問了極光的技術人員我的某一個問題之後給我的一個網頁鏈接裏面的,看到這我真的是悲喜交加,我瞬間精神了。當務之急是立馬聯系我們項目的後臺,確定是否給推送的加了content-available字段,自己也打印日誌看了一番,果然,在極光的後臺推送是沒有content-available字段,但是我們的php後臺給我們iOS這邊加上了這個字段,後臺也回復我他加了。在這裏告訴你,你千萬別信後臺說什麽去不掉,改不了這個字段的鬼話,在極光技術人員的幫助下,我復制各種能解決的方法,甚至代碼樣例給後臺,最後終於還是給我解決了這個問題,終於能看見打印的日誌裏面沒有了content-available字段,終於看到了曙光。不要以為這就結束了,你這就錯了,還有一個地方得註意,app這邊還得註意前後臺,從上面的圖片也是可以看出來的,就是在下面的方法裏面得加一個前臺的判斷,不在前臺才去實行跳轉的邏輯。最後拿著iOS8,iOS9,iOS10,iOS11的手機各種測試,終於正常了,我只想說上面這個圖我太喜歡了!

  下面我把項目裏面相關的極光源碼(包括集成)粘貼出來,小夥伴們可以隨意看看

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  ......省略不寫
   // Required 註冊通知 我們項目是只支持iOS7以上的設備
    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            //categories
            [JPUSHService
             registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                 UIUserNotificationTypeSound |
                                                 UIUserNotificationTypeAlert)
             categories:nil];
    } else {
        //categories nil
        [JPUSHService
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert)
#else
             //categories nil
             categories:nil];
            [JPUSHService
             registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                 UIRemoteNotificationTypeSound |
                                                 UIRemoteNotificationTypeAlert)
#endif
             // Required
             categories:nil];
        }
    [JPUSHService setupWithOption:launchOptions appKey:@"482c5c296d169dcfbebec15b" channel:@"AppStore" apsForProduction:YES advertisingIdentifier:nil];
//自定義消息需要用到的
//    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
//    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
    //2.1.9版本新增獲取registration id block接口。
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID獲取成功:%@",registrationID);
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:registrationID forKey:JPushRegistrationID];
            [defaults synchronize];
        }
        else{
            NSLog(@"registrationID獲取失敗,code:%d",resCode);
        }
    }]; 
  ......省略不寫  
}

// 將極光推送得到的deviceToken傳給SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required
    NSLog(@"iOS6?????");
    [JPUSHService handleRemoteNotification:userInfo];
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void
                        (^)(UIBackgroundFetchResult))completionHandler {
    // IOS 7 Support Required 
    if (application.applicationState != UIApplicationStateActive) {//不在前臺,必須判斷
        [self didJpushJumpToDetail:userInfo];
    }
   
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

//IOS 10 點擊通知走這裏
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSLog(@"iOS10以後處理後臺點擊消息通知的代理方法");
    
    // Required
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    [self didJpushJumpToDetail:userInfo];

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系統要求執行這個方法
}

//實現點擊通知跳轉到相應的詳情界面
- (void)didJpushJumpToDetail:(NSDictionary *)userInfo {
    NSLog(@"userInfo = %@", userInfo);
    int t = [[userInfo objectForKey:@"type"] intValue];
    if (t == 2) {// 1:即時問答 2:問題廣場
        NSString *type= [NSString stringWithFormat:@"%d",t];
        if ([userInfo objectForKey:@"qid"] && [userInfo objectForKey:@"waitType"]) {// && [userInfo objectForKey:@"rid"]
            NSString *qid = [NSString stringWithFormat:@"%d", [[userInfo objectForKey:@"qid"] intValue]];
            NSString *rid = [NSString stringWithFormat:@"%d", [[userInfo objectForKey:@"rid"] intValue]];
            NSString *waitType = [userInfo objectForKey:@"waitType"];//zhuiwen  ques
            [[NSNotificationCenter defaultCenter]postNotificationName:@"notificationGotoVc" object:nil userInfo:@{@"t":type, @"qid":qid, @"waitType":waitType, @"rid":rid}];
        }
    }else if(t == 1) {
        NSString *type= [NSString stringWithFormat:@"%d",t];
        if ([userInfo objectForKey:@"qid"] && [userInfo objectForKey:@"uid"] && [userInfo objectForKey:@"qtype"]) {
            
            NSString *qid = [NSString stringWithFormat:@"%d",[[userInfo objectForKey:@"qid"] intValue]];
            NSString *uid = [NSString stringWithFormat:@"%d",[[userInfo objectForKey:@"uid"] intValue]];
            NSString *qtype = [NSString stringWithFormat:@"%d",[[userInfo objectForKey:@"qtype"] intValue]];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"notificationGotoVc" object:nil userInfo:@{@"t":type, @"qid":qid, @"uid":uid, @"qtype":qtype}];
        }
    }
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSLog(@"iOS10新增:處理前臺收到通知的代理方法");
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }else {
        //應用處於前臺時的本地推送接受
    }
//註意:::註釋掉下面這行,則app在前臺不會展示頂部通知,如果去掉註釋則是會在頂部展示通知的,根據項目需求而定,我們要求不展示
//    completionHandler(UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置
}

點擊極光推送,實現跳轉