iOS極光推送部分

研究了下極光推送,最惡心的部分莫過(guò)于一系列證書的配置,
1、注意下p12的導(dǎo)出,特別需要注意,
正確方式是這樣的

正確導(dǎo)出方式,直接導(dǎo)出

錯(cuò)誤方式是這樣的
錯(cuò)誤方式,打開后導(dǎo)出

2、xcode 的配置

3、導(dǎo)入極光推送sdk,同時(shí),添加依賴庫(kù)

  CFNetwork.framework
  CoreFoundation.framework
  CoreTelephony.framework
  SystemConfiguration.framework
  CoreGraphics.framework
  Foundation.framework
  UIKit.framework
  Security.framework
  libz.tbd (Xcode7以下版本是libz.dylib)
  AdSupport.framework (獲取IDFA需要;如果不使用IDFA,請(qǐng)不要添加)
  UserNotifications.framework (Xcode8及以上)
  libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)

真機(jī)編譯,沒(méi)報(bào)錯(cuò),好的接下來(lái)開始敲代碼
4、在AppDelegate.m里面添加 JPUSHRegisterDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        //iOS10以上
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

    } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //iOS8以上可以添加自定義categories
        [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) categories:nil];
    } else {
        //iOS8以下categories 必須為nil
        [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)categories:nil];
        
    }
    
    BOOL isProduction = NO;// NO為開發(fā)環(huán)境,YES為生產(chǎn)環(huán)境
    //廣告標(biāo)識(shí)符
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    //Required(2.1.5版本的SDK新增的注冊(cè)方法,改成可上報(bào)IDFA,如果沒(méi)有使用IDFA直接傳nil
    [JPUSHService setupWithOption:launchOptions appKey:APP_KEY
                          channel:nil
                 apsForProduction:isProduction
            advertisingIdentifier:advertisingId];
    //此處設(shè)置當(dāng)點(diǎn)擊icon時(shí)候進(jìn)入app 銷毀icon badge
    NSInteger bagValue = [UIApplication sharedApplication].applicationIconBadgeNumber;
    NSLog(@"bagValue -- %ld", bagValue);
    if (bagValue != 0) {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
        [JPUSHService setBadge:0];
        [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
        [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
        
    }    
    return YES;
}
#pragma mark -- 注冊(cè) DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [JPUSHService registerDeviceToken:deviceToken];
}
#pragma mark -- JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler
{
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    NSLog(@"userInfo -- %@", userInfo);
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]
        ]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)這個(gè)方法,選擇是否提醒戶,有Badge、Sound、Alert三種類型可以選擇設(shè)置
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    NSLog(@"00000userInfo -- %@", userInfo);
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(); // 系統(tǒng)要求執(zhí)行這個(gè)方法
}
#pragma mark -- iOS 7 遠(yuǎn)程請(qǐng)求
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark -- iOS 6 或者以下遠(yuǎn)程請(qǐng)求
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [JPUSHService handleRemoteNotification:userInfo];
}

接下來(lái),我們從極光后臺(tái),推送一條消息過(guò)來(lái),打印出來(lái)的userInfo

userInfo -- {
    "_j_msgid" = 2929855611;
    aps =     {
        alert = "775wode ";
        badge = 1;
        sound = default;
    };
}

我們看到 badge = 1,同時(shí)受到的消息為 alert = "775wode"
好的,我們?cè)侔l(fā)送一條消息

有比較就有發(fā)現(xiàn)

badge 值一直為1,同時(shí),我們發(fā)送了倆條消息,但是icon角標(biāo)上面一直顯示為1
這樣解決,我們直接在極光的后臺(tái)設(shè)置badge值為 +1這樣每次發(fā)送消息的時(shí)候,在之前的基礎(chǔ)上面加1,數(shù)量變正常
直接后臺(tái)設(shè)置咯

以上,就是集成極光推送發(fā)送遠(yuǎn)程通知的內(nèi)容,個(gè)人認(rèn)為,集成不難,難的是,集成之前各種證書的配置,生產(chǎn)環(huán)境,開發(fā)環(huán)境,各種煩躁,希望大家一起加油?。?!

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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