Xcode8 、iOS10的極光推送

一、申請(qǐng)證書(shū)

申請(qǐng)證書(shū)的過(guò)程在這里就不多說(shuō)了,請(qǐng)自行查閱資料。
證書(shū)申請(qǐng)好并下載后,在極光的官網(wǎng)上傳證書(shū)(p12文件)。

二、打開(kāi)推送開(kāi)關(guān)

勾選推送選項(xiàng)

然后我們會(huì)看到一個(gè)這樣的文件出現(xiàn)了,這個(gè)不需要理它。

三、導(dǎo)入SDK并完善代碼

準(zhǔn)備工作做好了,就可以把下載好的SDK導(dǎo)入工程,開(kāi)始寫(xiě)代碼了。

這時(shí)候可能會(huì)報(bào)錯(cuò):


導(dǎo)入SDK之后報(bào)錯(cuò)

需要添加一個(gè)庫(kù):libresolv.tbd ,就可以解決問(wèn)題了:

就是它

下面就開(kāi)始寫(xiě)代碼了(AppDelegate.m):

#import <UserNotifications/UserNotifications.h>

@interface AppDelegate ()<JPUSHRegisterDelegate,UNUserNotificationCenterDelegate>

注冊(cè)通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; //Required  
    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) {  
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |  
                                                          UIUserNotificationTypeSound |  
                                                          UIUserNotificationTypeAlert) categories:nil];  
    }  
    else  
    {  
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |  
                                                          UIUserNotificationTypeSound |  
                                                          UIUserNotificationTypeAlert) categories:nil];  
    }  
    [JPUSHService setupWithOption:dict  
                           appKey:APPKEY  
                          channel:nil  
                 apsForProduction:NO  
            advertisingIdentifier:advertisingId];  
}

注冊(cè)推送,推送是否成功:

//注冊(cè)推送  
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
{  
    // Required  
    [JPUSHService registerDeviceToken:deviceToken];  
}  
  
//注冊(cè)遠(yuǎn)程通知失敗  
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {  
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);  
}  

接收推送

//iOS10 以前
//接收推送  
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
{  
    // Required  
    [JPUSHService handleRemoteNotification:userInfo];  
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];  
    NSLog(@"收到通知:%@", [self logDic:userInfo]);  
}  
//推送接收完成  
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {  
      
    // IOS 7 Support Required  
    [JPUSHService handleRemoteNotification:userInfo];  
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];  
    completionHandler(UIBackgroundFetchResultNewData);  
    NSLog(@"收到通知:%@", [self logDic:userInfo]);  
    _userInfo = userInfo;  
}  

#pragma mark- JPUSHRegisterDelegate  
// iOS 10 Support  
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {  
    NSDictionary * userInfo = notification.request.content.userInfo;  
    UNNotificationRequest *request = notification.request; // 收到推送的請(qǐng)求  
    UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容  
    NSNumber *badge = content.badge;  // 推送消息的角標(biāo)  
    NSString *body = content.body;    // 推送消息體  
    UNNotificationSound *sound = content.sound;  // 推送消息的聲音  
    NSString *subtitle = content.subtitle;  // 推送消息的副標(biāo)題  
    NSString *title = content.title;  // 推送消息的標(biāo)題  
      
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {  
        NSLog(@"iOS10 前臺(tái)收到遠(yuǎn)程通知:%@", [self logDic:userInfo]);  
    }  
    else {  
        // 判斷為本地通知  
        NSLog(@"iOS10 前臺(tái)收到本地通知:{\\nbody:%@,\\ntitle:%@,\\nsubtitle:%@,\\nbadge:%@,\\nsound:%@,\\nuserInfo:%@\\n}",body,title,subtitle,badge,sound,userInfo);  
    }  
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);  
    // 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶(hù),有Badge、Sound、Alert三種類(lèi)型可以設(shè)置  
}  
  
// 通知的點(diǎn)擊事件  
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{  
      
    NSDictionary * userInfo = response.notification.request.content.userInfo;  
    UNNotificationRequest *request = response.notification.request; // 收到推送的請(qǐng)求  
    UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容  
    NSNumber *badge = content.badge;  // 推送消息的角標(biāo)  
    NSString *body = content.body;    // 推送消息體  
    UNNotificationSound *sound = content.sound;  // 推送消息的聲音  
    NSString *subtitle = content.subtitle;  // 推送消息的副標(biāo)題  
    NSString *title = content.title;  // 推送消息的標(biāo)題  
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {  
        NSLog(@"iOS10 收到遠(yuǎn)程通知:%@", [self logDic:userInfo]);  
    }  
    else  
    {  
        // 判斷為本地通知  
        NSLog(@"iOS10 收到本地通知:{\\nbody:%@,\\ntitle:%@,\\nsubtitle:%@,\\nbadge:%@,\\nsound:%@,\\nuserInfo:%@\\n}",body,title,subtitle,badge,sound,userInfo);  
    }  
    // 系統(tǒng)要求執(zhí)行這個(gè)方法  
    completionHandler();  
}  

到此為止,iOS7-iOS10 的推送就做完了。

最后編輯于
?著作權(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)容