蘋(píng)果在iOS10上對(duì)apns推送做了修改, 極光也是很給力的, 在第一時(shí)間就對(duì)sdk進(jìn)行了更新, 下面對(duì)iOS10注冊(cè)極光推送進(jìn)行一下記錄.
首先, 在極光的開(kāi)發(fā)者服務(wù)里注冊(cè)應(yīng)用獲取appKey, 在apple Developer配置推送證書(shū)...等等等這些廢話就不說(shuō)了.
兼容iOS10的是極光2.1.9版本的sdk.
1. 導(dǎo)入SDK
2. 導(dǎo)入SDK依賴的系統(tǒng)框架
CFNetwork.framework
CoreFoundation.frameworkCoreTelephony.frameworkSystemConfiguration.frameworkCoreGraphics.frameworkFoundation.frameworkUIKit.frameworkSecurity.framework
Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib
Adsupport.framework (獲取IDFA需要;如果不使用IDFA,請(qǐng)不要添加)
UserNotifications.framework(Xcode8及以上)
3. 設(shè)置Build Setting中, Search Paths的User Header Search Paths
4.如果用的是Xcode8及以上環(huán)境開(kāi)發(fā)需要開(kāi)啟Application Target的Capabilities->Push Notifications選項(xiàng)
這兩個(gè)一定要都是對(duì)號(hào) , ?這個(gè)選項(xiàng)不開(kāi)啟在iOS10后不會(huì)注冊(cè)成功
添加這個(gè)選項(xiàng)會(huì)在項(xiàng)目中多這樣一個(gè)文件
5. 不要忘記Xcode7以上需要支持http傳輸方式
下面是需要寫(xiě)的代碼部分:
6. 在AppDelegate.m中, 引入頭文件
[objc]view plaincopy
//?極光推送
#import?"JPUSHService.h"
#import?
#ifdef?NSFoundationVersionNumber_iOS_9_x_Max
#import??//?這里是iOS10需要用到的框架
#endif
7. 設(shè)置注冊(cè)極光推送需要的一些參數(shù)
[objc]view plaincopy
staticNSString*constJPUSHAPPKEY?=@"xxxxxxxxxxxxxxxxx";//?極光appKey
staticNSString*constchannel?=@"Publish?channel";//?固定的
#ifdef?DEBUG?//?開(kāi)發(fā)
staticBOOLconstisProduction?=?FALSE;//?極光FALSE為開(kāi)發(fā)環(huán)境
#else?//?生產(chǎn)
staticBOOLconstisProduction?=?TRUE;//?極光TRUE為生產(chǎn)環(huán)境
#endif
8. 這里是AppDelegate.m中的代碼, 分了幾大塊, 全部粘到下面, 直接復(fù)制可用(只需要下面這些代碼就可以實(shí)現(xiàn)通知)
[objc]view plaincopy
//
//??AppDelegate.m
//??iOS10_JPUSH
//
//??Created?by?周昊?on?16/9/18.
//??Copyright???2016年?周昊.?All?rights?reserved.
//
#import?"AppDelegate.h"
//?極光推送
#import?"JPUSHService.h"
#import?
#ifdef?NSFoundationVersionNumber_iOS_9_x_Max
#import??//?這里是iOS10需要用到的框架
#endif
staticNSString*constJPUSHAPPKEY?=@"xxxxxxxxxxxxxxxxx";//?極光appKey
staticNSString*constchannel?=@"Publish?channel";//?固定的
#ifdef?DEBUG?//?開(kāi)發(fā)
staticBOOLconstisProduction?=?FALSE;//?極光FALSE為開(kāi)發(fā)環(huán)境
#else?//?生產(chǎn)
staticBOOLconstisProduction?=?TRUE;//?極光TRUE為生產(chǎn)環(huán)境
#endif
@interfaceAppDelegate?()//?最新版的sdk需要實(shí)現(xiàn)這個(gè)代理方法
@end
@implementationAppDelegate
-?(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions?{
//?注冊(cè)apns通知
if([[UIDevicecurrentDevice].systemVersionfloatValue]?>=10.0)//?iOS10
{
#ifdef?NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity*?entity?=?[[JPUSHRegisterEntityalloc]init];
entity.types=?UNAuthorizationOptionAlert|UNAuthorizationOptionBadge?|?UNAuthorizationOptionSound;
[JPUSHServiceregisterForRemoteNotificationConfig:entitydelegate:self];
#endif
}
elseif([[UIDevicecurrentDevice].systemVersionfloatValue]?>=8.0)//?iOS8,?iOS9
{
//可以添加自定義categories
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge?|?UIUserNotificationTypeSound?|?UIUserNotificationTypeAlert)categories:nil];
}
else//?iOS7
{
//categories?必須為nil
[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge?|?UIRemoteNotificationTypeSound?|?UIRemoteNotificationTypeAlert)categories:nil];
}
/*
*??launchingOption?啟動(dòng)參數(shù).
*??appKey?一個(gè)JPush?應(yīng)用必須的,唯一的標(biāo)識(shí).
*??channel?發(fā)布渠道.?可選.
*??isProduction?是否生產(chǎn)環(huán)境.?如果為開(kāi)發(fā)狀態(tài),設(shè)置為?NO;?如果為生產(chǎn)狀態(tài),應(yīng)改為?YES.
*??advertisingIdentifier?廣告標(biāo)識(shí)符(IDFA)?如果不需要使用IDFA医舆,傳nil.
*?此接口必須在?App?啟動(dòng)時(shí)調(diào)用,?否則?JPush?SDK?將無(wú)法正常工作.
*/
//?廣告標(biāo)識(shí)符
NSString*advertisingId?=?[[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];
//?如不需要使用IDFA俘侠,advertisingIdentifier?可為nil
//?注冊(cè)極光推送
[JPUSHServicesetupWithOption:launchOptionsappKey:JPUSHAPPKEYchannel:channelapsForProduction:isProductionadvertisingIdentifier:advertisingId];
//2.1.9版本新增獲取registration?id?block接口。
[JPUSHServiceregistrationIDCompletionHandler:^(intresCode,NSString*registrationID)?{
if(resCode?==0)
{
//?iOS10獲取registrationID放到這里了,?可以存到緩存里,?用來(lái)標(biāo)識(shí)用戶單獨(dú)發(fā)送推送
NSLog(@"registrationID獲取成功:%@",registrationID);
[[NSUserDefaultsstandardUserDefaults]setObject:registrationIDforKey:@"registrationID"];
[[NSUserDefaultsstandardUserDefaults]synchronize];
}
else
{
NSLog(@"registrationID獲取失敗蔬将,code:%d",resCode);
}
}];
returnYES;
}
//?---------------------------------------------------------------------------------
-?(void)applicationWillResignActive:(UIApplication*)application?{
}
-?(void)applicationDidEnterBackground:(UIApplication*)application?{
[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];
}
-?(void)applicationWillEnterForeground:(UIApplication*)application?{
[applicationsetApplicationIconBadgeNumber:0];
[applicationcancelAllLocalNotifications];
}
-?(void)applicationDidBecomeActive:(UIApplication*)application?{
}
-?(void)applicationWillTerminate:(UIApplication*)application?{
}
//?---------------------------------------------------------------------------------
#pragma?mark?-?注冊(cè)推送回調(diào)獲取?DeviceToken
#pragma?mark?--?成功
-?(void)application:(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
//?注冊(cè)成功
//?極光:?Required?-?注冊(cè)?DeviceToken
[JPUSHServiceregisterDeviceToken:deviceToken];
}
#pragma?mark?--?失敗
-?(void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
//?注冊(cè)失敗
NSLog(@"did?Fail?To?Register?For?Remote?Notifications?With?Error:?%@",?error);
}
//?---------------------------------------------------------------------------------
//?這部分是官方demo里面給的,?也沒(méi)實(shí)現(xiàn)什么功能,?放著以備不時(shí)之需
#if?__IPHONE_OS_VERSION_MAX_ALLOWED?>?__IPHONE_7_1
-?(void)application:(UIApplication*)applicationdidRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings
{
}
//?Called?when?your?app?has?been?activated?by?the?user?selecting?an?action?from
//?a?local?notification.
//?A?nil?action?identifier?indicates?the?default?action.
//?You?should?call?the?completion?handler?as?soon?as?you've?finished?handling
//?the?action.
-?(void)application:(UIApplication*)applicationhandleActionWithIdentifier:(NSString*)identifierforLocalNotification:(UILocalNotification*)notificationcompletionHandler:(void(^)())completionHandler
{
}
//?Called?when?your?app?has?been?activated?by?the?user?selecting?an?action?from
//?a?remote?notification.
//?A?nil?action?identifier?indicates?the?default?action.
//?You?should?call?the?completion?handler?as?soon?as?you've?finished?handling
//?the?action.
-?(void)application:(UIApplication*)applicationhandleActionWithIdentifier:(NSString*)identifierforRemoteNotification:(NSDictionary*)userInfocompletionHandler:(void(^)())completionHandler
{
}
#endif
//?---------------------------------------------------------------------------------
-?(void)application:(UIApplication*)applicationdidReceiveLocalNotification:(UILocalNotification*)notification
{
[JPUSHServiceshowLocalNotificationAtFront:notificationidentifierKey:nil];
}
//?---------------------------------------------------------------------------------
#pragma?mark?-?iOS7:?收到推送消息調(diào)用
-?(void)application:(UIApplication*)applicationdidReceiveRemoteNotification:(NSDictionary*)userInfofetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler?{
//?iOS7之后調(diào)用這個(gè)
[JPUSHServicehandleRemoteNotification:userInfo];
NSLog(@"iOS7及以上系統(tǒng)爷速,收到通知");
if([[UIDevicecurrentDevice].systemVersionfloatValue]?<10.0||?application.applicationState>0)
{
//?程序在前臺(tái)或通過(guò)點(diǎn)擊推送進(jìn)來(lái)的會(huì)彈這個(gè)alert
NSString*message?=?[NSStringstringWithFormat:@"iOS7-8-9收到的推送%@",?[userInfo[@"aps"]objectForKey:@"alert"]];
UIAlertView*alert?=?[[UIAlertViewalloc]initWithTitle:@"提示"message:messagedelegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nilnil];
[alertshow];
}
completionHandler(UIBackgroundFetchResultNewData);
}
//?---------------------------------------------------------------------------------
#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*)centerwillPresentNotification:(UNNotification*)notificationwithCompletionHandler:(void(^)(NSInteger))completionHandler?{
NSDictionary*?userInfo?=?notification.request.content.userInfo;
if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]])
{
[JPUSHServicehandleRemoteNotification:userInfo];
NSString*message?=?[NSStringstringWithFormat:@"will%@",?[userInfo[@"aps"]objectForKey:@"alert"]];
NSLog(@"iOS10程序在前臺(tái)時(shí)收到的推送:?%@",?message);
UIAlertView*alert?=?[[UIAlertViewalloc]initWithTitle:@"提示"message:messagedelegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nilnil];
[alertshow];
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);//?需要執(zhí)行這個(gè)方法,選擇是否提醒用戶霞怀,有Badge惫东、Sound、Alert三種類(lèi)型可以設(shè)置
}
//?程序關(guān)閉后,?通過(guò)點(diǎn)擊推送彈出的通知
-?(void)jpushNotificationCenter:(UNUserNotificationCenter*)centerdidReceiveNotificationResponse:(UNNotificationResponse*)responsewithCompletionHandler:(void(^)())completionHandler?{
NSDictionary*?userInfo?=?response.notification.request.content.userInfo;
if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]])
{
[JPUSHServicehandleRemoteNotification:userInfo];
NSString*message?=?[NSStringstringWithFormat:@"did%@",?[userInfo[@"aps"]objectForKey:@"alert"]];
NSLog(@"iOS10程序關(guān)閉后通過(guò)點(diǎn)擊推送進(jìn)入程序彈出的通知:?%@",?message);
UIAlertView*alert?=?[[UIAlertViewalloc]initWithTitle:@"提示"message:messagedelegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nilnil];
[alertshow];
}
completionHandler();//?系統(tǒng)要求執(zhí)行這個(gè)方法
}
#endif
@end
注: 極光的AppKey要自己到極光的官網(wǎng)申請(qǐng)哦