1. 程式人生 > >極光推送的坑

極光推送的坑

極光推送開發環境可以收到,生產環境收不到推送

初次發現這個問題的時候 首先排查的就是證書配置。

綠色的表示沒什麼問題。

開發環境的證書跟生產環境的證書 都已經配置完成,

下面我們看看極光官網的配置

這個也沒有問題。 然後看程式碼

//極光植入

    [JPUSHService setBadge:0]; //JPush伺服器角標置為0

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];//app角標置為0

    //極光推送

    //notice: 3.0.0及以後版本註冊可以這樣寫,也可以繼續用之前的註冊方式

    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];

    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

        // 可以新增自定義categories

        // NSSet<UNNotificationCategory *> *categories for iOS10 or later

        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9

    }

    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

    

    //JAppKey : 極光推送申請下來的appKey Jchannel : 可以直接設定預設值即可 Publish channel

    //是否是開啟的

    BOOL JPush = NO;

    if ([kMemberLoginProductFlag isEqualToString:@"YES"])

    {

        JPush = YES;

    }

    else{

        JPush = NO;

    }

    

    [JPUSHService setupWithOption:launchOptions appKey:JiGuangAppKey

                          channel:nil apsForProduction:JPush]; //如果是生產環境應該設定為YES

 

#pragma mark - 向伺服器上報Device Token    極光代理

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    

    // Required - 註冊 DeviceToken

    

    [JPUSHService registerDeviceToken:deviceToken];

    

}

#pragma mark - 極光推送代理

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {

    

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

    DLog(@"@@@@@@@進來了進來了  %@",userInfo);

    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  {

        [JPUSHService handleRemoteNotification:userInfo];

    }

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

}

 

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

    

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

    DLog(@"@@@@@@@進來了進來了  %@",userInfo);

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

        [JPUSHService handleRemoteNotification:userInfo];

        

    }

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

    [JPUSHService setBadge:0]; //JPush伺服器角標置為0

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];//app角標置為0

    

    //這裡是自定義了 。。。。。。

    if([UIApplication sharedApplication].applicationState == UIApplicationStateActive || [UIApplication sharedApplication].applicationState == UIApplicationStateBackground)

    {

        DLog(@"@@@@@@@進來了");

        

        [self pushViewControllerWith:userInfo];

    }else

    {

        

//        if (![USER_DEFAULT valueForKey:kNotificationPushDictionary])

//        {

//            [self pushViewControllerWith:userInfo];

//        }

        DLog(@"@@@@@@@進來了進來了");

                [self pushViewControllerWith:userInfo];

    }

}

程式碼看了半天也沒啥問題。最後發現是打包的問題,測試環境直接打包就行。

但是生產的 必須一定要adhoc打包才能測試 其他方式打包安裝不行。坑啊 。

其次,前後臺所有的sdk版本要可以對的上的。版本不統一也可能出現推送收不到的問題。瞎忙了2天,快上線了,頭都大了。