極光推送

  • 照著文檔來(lái)就只缺一個(gè)獲取devicetoken,上代碼唄

#pragma mark - 極光推送

- (void)JPush:(NSDictionary *)launchOptions {
    [self JPushAddAPNs];
    [self JPushInit:launchOptions];
}

// 添加初始化APNs代碼
- (void)JPushAddAPNs {
    
    if (GETCURRENTVERSION >= 10.0) {
        JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    } else if (GETCURRENTVERSION >= 8.0) {
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
    } else {
        // 不在支持8.0以下
    }
}

// 初始化JPUsh代碼 設(shè)置的是開(kāi)發(fā)模式
- (void)JPushInit:(NSDictionary *)launchOptions {
    // 獲取IDFA 沒(méi)有可直接傳nil
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    
    // channel是分發(fā)路徑 一個(gè)是什么模式 adverid可有可無(wú)
   [JPUSHService setupWithOption:launchOptions appKey:JPushAppKey channel:@"" apsForProduction:NO advertisingIdentifier:advertisingId];
}

// 注冊(cè)APNs失敗接口
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

// 獲取deviceToken  環(huán)信也需要在這兒獲取給SDK
/*****************這個(gè)在極光的文檔里并沒(méi)有********************/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    [JPUSHService registerDeviceToken:deviceToken];
}

#pragma mark - JPUSHRegisterDelegate

// iOS 10 Support 提示后臺(tái)全部包在一個(gè)數(shù)組里 這個(gè)安卓可以成功iOS可能存在問(wèn)題 但是是后臺(tái)的錯(cuò)
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 系統(tǒng)要求執(zhí)行額這個(gè)方法 選擇是否提醒用戶(hù),有Badge、Sound、Alert三種類(lèi)型可以選擇設(shè)置
}

/**********     文檔也沒(méi)有 但是Appdelegate有提示     ***************/
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系統(tǒng)要求執(zhí)行這個(gè)方法
}

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

// 環(huán)信接受到的推送 極光接收的推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [JPUSHService handleRemoteNotification:userInfo];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容