極光推送配置以及應(yīng)用

1、導(dǎo)入 SDK

Cocoapods 導(dǎo)入,通過(guò) Cocoapods 下載地址:pod 'Push'

2、使用 Xcode 8 及以上環(huán)境開(kāi)發(fā)--開(kāi)啟通知

開(kāi)啟

3、將以下代碼添加到 AppDelegate.m 引用頭文件的位置。

//引入JPush功能所需頭文件

#import "JPUSHService.h"

// iOS10注冊(cè)APNs所需頭文件

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import <UserNotifications/UserNotifications.h>

#endif

// 如果需要使用 idfa 功能所需要引入的頭文件(可選)

#import <AdSupport/AdSupport.h>

4、添加代理 <JPUSHRegisterDelegate>

5、初始化APNS代碼

將以下代碼添加到 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

//Required

? //notice: 3.0.0及以后版本注冊(cè)可以這樣寫(xiě),也可以繼續(xù)用之前的注冊(cè)方式

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

? entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;

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

? ? //可以添加自定義categories

? ? // NSSet *categories for iOS10 or later

? ? // NSSet *categories for iOS8 and iOS9

? }

? [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

// Optional

? //獲取IDFA

? //如需使用IDFA功能請(qǐng)?zhí)砑哟舜a并在初始化方法的advertisingIdentifier參數(shù)中填寫(xiě)對(duì)應(yīng)值

? NSString *advertisingId =[[[ASIdentifierManager sharedManager]advertisingIdentifier]UUIDString];

? // Required

? // init Push

? // notice: 2.1.5版本的SDK新增的注冊(cè)方法,改成可上報(bào)IDFA,如果沒(méi)有使用IDFA直接傳nil

? //如需繼續(xù)使用pushConfig.plist文件聲明appKey等配置內(nèi)容,請(qǐng)依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化。

? [JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];

部分參數(shù)說(shuō)明:

appKey:(極光推送創(chuàng)建應(yīng)用的appKey)

channel:自定義如:App Store。

apsForProduction:

1.3.1 版本新增,用于標(biāo)識(shí)當(dāng)前應(yīng)用所使用的 APNs 證書(shū)環(huán)境。

0(默認(rèn)值)表示采用的是開(kāi)發(fā)證書(shū),1 表示采用生產(chǎn)證書(shū)發(fā)布應(yīng)用。

注:此字段的值要與Build Settings的Code Signing配置的證書(shū)環(huán)境一致。

advertisingIdentifier: IDFA

IDFA

6、傳deviceToken

-(void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

? /// Required -注冊(cè)DeviceToken

? [JPUSHService registerDeviceToken:deviceToken];

}


7、注冊(cè)APNS失敗方法

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

?//Optional

? NSLog(@"did Fail To Register For Remote Notifications With Error: %@",error);

}

8、添加處理 APNs 通知回調(diào)方法,在 AppDelegate.m 實(shí)現(xiàn)該回調(diào)方法并添加回調(diào)方法中的代碼

#pragma mark- JPUSHRegisterDelegate

// iOS 12 Support

-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{

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

? ? //從通知界面直接進(jìn)入應(yīng)用

? }else{

? ? //從通知設(shè)置界面進(jìn)入應(yīng)用

? }

}

// iOS 10 Support

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

? // Required

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

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

? ? [JPUSHService handleRemoteNotification:userInfo];

? }

? completionHandler(UNNotificationPresentationOptionAlert);//需要執(zhí)行這個(gè)方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設(shè)置

}

// iOS 10 Support

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

? // Required

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

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

? ? [JPUSHService handleRemoteNotification:userInfo];

? }

? completionHandler();? //系統(tǒng)要求執(zhí)行這個(gè)方法

}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

? // Required,iOS 7 Support

? [JPUSHService handleRemoteNotification:userInfo];

? completionHandler(UIBackgroundFetchResultNewData);

}

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

? // Required,For systems with less than or equal to iOS 6

? [JPUSHService handleRemoteNotification:userInfo];

}


更詳細(xì)的配置文檔參考?iOS 極光推送SDK集成指南

?著作權(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)容