解析iOS10中的極光推送消息的適配
iOS10發(fā)布后,發(fā)現(xiàn)項(xiàng)目中的極光推送接收消息異常了。
查了相關(guān)資料后才發(fā)現(xiàn),iOS10中對(duì)于通知做了不少改變。同時(shí)也發(fā)現(xiàn)極光也很快更新了對(duì)應(yīng)的SDK。
現(xiàn)在就把適配修改的做法分享一下,希望對(duì)有需要的童鞋有所幫助。
具體做法如下:
注意:必須先安裝Xcode8.0版本。
一、添加相關(guān)的SKD,或framework文件
1、添加UserNotification.framework

2、更新jpush的SDK(最新版本:jpush-ios-2.1.9.a)https://www.jiguang.cn

二、進(jìn)行路徑和消息推送的配置
1、設(shè)置jpush的SDK的路徑

2、開(kāi)啟消息推送功能

三、代碼修改
1、添加userNotification的頭文件

2、添加userNotification的啟用代碼

3、添加jpush的適配代碼

4、添加jpush的代理和代理方法(注意:在appDelegate.m文件中使用)


補(bǔ)充:完整的使用極光
1、導(dǎo)入相應(yīng)頭文件
#import "JPUSHService.h" #import <AdSupport/AdSupport.h> #ifdef NSFoundationVersionNumber_iOS_9_x_Max // 這里是iOS10需要用到的框架 #import <UserNotifications/UserNotifications.h> #endif
2、啟動(dòng)極光推送功能
static NSString *JPushAppKey = @"6abc87b33b23d35b9c3b86e0";
static NSString *JPushChannel = @"Publish channel";
// static BOOL JPushIsProduction = NO;
#ifdef DEBUG
// 開(kāi)發(fā) 極光FALSE為開(kāi)發(fā)環(huán)境
static BOOL const JPushIsProduction = FALSE;
#else
// 生產(chǎn) 極光TRUE為生產(chǎn)環(huán)境
static BOOL const JPushIsProduction = TRUE;
#endif
[objc] view plain copy 在CODE上查看代碼片派生到我的代碼片
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 啟動(dòng)極光推送
// Required
// - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10
{
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
entity.types = (UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);
[JPUSHService registerForRemoteNotificationConfig:entity delegate:target];
#endif
}
else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
// categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)
categories:nil];
}
else
{
// categories nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)
categories:nil];
}
// Required
// [JPUSHService setupWithOption:launchOptions]
// pushConfig.plist appKey
// 有廣告符標(biāo)識(shí)IDFA(盡量不用,避免上架審核被拒)
/*
NSString *JPushAdvertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
[JPUSHService setupWithOption:JPushOptions
appKey:JPushAppKey
channel:JPushChannel
apsForProduction:JPushIsProduction
advertisingIdentifier:JPushAdvertisingId];
*/
// 或無(wú)廣告符標(biāo)識(shí)IDFA(盡量不用,避免上架審核被拒)
[JPUSHService setupWithOption:options
appKey:JPushAppKey
channel:JPushChannel
apsForProduction:JPushIsProduction];
// 2.1.9版本新增獲取registration id block接口。
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0)
{
// iOS10獲取registrationID放到這里了, 可以存到緩存里, 用來(lái)標(biāo)識(shí)用戶(hù)單獨(dú)發(fā)送推送
NSLog(@"registrationID獲取成功:%@",registrationID);
[[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
NSLog(@"registrationID獲取失敗,code:%d",resCode);
}
}];
return YES;
}
3、注冊(cè)
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[JPUSHService registerDeviceToken:data];
}
4、注冊(cè)失敗
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificwationsWithError:(NSError *)error
{
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
5、接收
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// apn 內(nèi)容獲?。?
// 取得 APNs 標(biāo)準(zhǔn)信息內(nèi)容
[JPUSHService handleRemoteNotification:dict];
}
6、處理通知
6-1、iOS10以下版本時(shí)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
DLog(@"2-1 didReceiveRemoteNotification remoteNotification = %@", userInfo);
// apn 內(nèi)容獲取:
[JPUSHService handleRemoteNotification:dict];
completionHandler(UIBackgroundFetchResultNewData);
DLog(@"2-2 didReceiveRemoteNotification remoteNotification = %@", userInfo);
if ([userInfo isKindOfClass:[NSDictionary class]])
{
NSDictionary *dict = userInfo[@"aps"];
NSString *content = dict[@"alert"];
DLog(@"content = %@", content);
}
if (application.applicationState == UIApplicationStateActive)
{
// 程序當(dāng)前正處于前臺(tái)
}
else if (application.applicationState == UIApplicationStateInactive)
{
// 程序處于后臺(tái)
}
}
6-2、iOS10及以上版本時(shí)
#pragma mark - iOS10: 收到推送消息調(diào)用(iOS10是通過(guò)Delegate實(shí)現(xiàn)的回調(diào))
#pragma mark- JPUSHRegisterDelegate
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
// 當(dāng)程序在前臺(tái)時(shí), 收到推送彈出的通知
- (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];
}
// 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶(hù),有Badge、Sound、Alert三種類(lèi)型可以設(shè)置
// completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
// 程序關(guān)閉后, 通過(guò)點(diǎn)擊推送彈出的通知
- (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è)方法
}
#endif
7、其他注意事項(xiàng)
為了保證用戶(hù)能正常接收,或有針對(duì)性的接收通知,登錄成功后(或退出后)需要設(shè)置別名、標(biāo)記。通常都是該邏輯都是寫(xiě)在用戶(hù)登錄APP成功之后,或者是用戶(hù)退出當(dāng)前登錄狀態(tài)后。
/// 綁定別名(注意:1 登錄成功或者自動(dòng)登錄后;2 去除綁定-退出登錄后)
+ (void)JPushTagsAndAliasInbackgroundTags:(NSSet *)set alias:(NSString *)name
{
// 標(biāo)簽分組(表示沒(méi)有值)
NSSet *tags = set;
// 用戶(hù)別名(自定義值,nil是表示沒(méi)有值)
NSString *alias = name;
NSLog(@"tags = %@, alias = %@(registrationID = %@)", tags, alias, [self registrationID]);
// tags、alias均無(wú)值時(shí)表示去除綁定
[JPUSHService setTags:tags aliasInbackground:alias];
}
以上所述是小編給大家介紹的解析iOS10中的極光推送消息的適配,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
IOS 開(kāi)發(fā)之操作圖庫(kù)自定義控制器
這篇文章主要介紹了IOS 開(kāi)發(fā)之操作圖庫(kù)自定義控制器的相關(guān)資料,需要的朋友可以參考下2017-02-02
UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法
這篇文章主要為大家詳細(xì)介紹了UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
iOS App開(kāi)發(fā)中的UIStackView堆疊視圖使用教程
UIStackView是iOS9以來(lái)新增加的組件,使我們能夠?qū)IView子類(lèi)對(duì)象進(jìn)行靈活排版,這里我們就來(lái)看一下iOS App開(kāi)發(fā)中的UIStackView堆疊視圖使用教程2016-07-07
iOS中利用UIBezierPath + CAAnimation實(shí)現(xiàn)心跳動(dòng)畫(huà)效果
這篇文章主要給大家介紹了關(guān)于iOS中利用UIBezierPath + CAAnimation實(shí)現(xiàn)心跳動(dòng)畫(huà)效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的日常開(kāi)發(fā)具有一定的參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
iOS開(kāi)發(fā)之MRC(手動(dòng)內(nèi)存管理)詳解
這篇文章主要介紹了?iOS開(kāi)發(fā)之MRC(手動(dòng)內(nèi)存管理)詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08
iOS UIWebView實(shí)現(xiàn)禁止用戶(hù)復(fù)制剪切功能
這篇文章主要給大家介紹了iOS中的UIWebView如何實(shí)現(xiàn)禁止用戶(hù)復(fù)制剪切的功能,文中給出了詳細(xì)的示例代碼,有需要的朋友們可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11

