如果app需要開啟遠(yuǎn)程推送功能,自己寫下來就會(huì)相對較麻煩,所以我選擇了極光推送
一.證書下載
這里的證書,是創(chuàng)建開發(fā)環(huán)境和生產(chǎn)環(huán)境的證書 ,來開啟你app的推送服務(wù)
首先進(jìn)入蘋果開發(fā)者中心
登錄之后點(diǎn)擊Certificates,Identifiers & Profiles

創(chuàng)建App ID

注意一點(diǎn):
App ID的名字可以隨便取,Bundle ID一定要注意,要推送的App項(xiàng)目中的Bundle ID一定要是這個(gè)Bundle ID

App Services可以現(xiàn)在勾上Push Notifications,也可以后面再改,然后點(diǎn)continue(建議直接勾,能做一次的事情何必做兩次呢)

勾選了Push Notifications,所有它現(xiàn)在是橙色的,不然應(yīng)該是灰色

出來后點(diǎn)擊你剛創(chuàng)建的App ID,然后點(diǎn)Edit

單擊你剛創(chuàng)建的App ID,點(diǎn)擊edit

創(chuàng)建開發(fā)環(huán)境和生產(chǎn)環(huán)境的證書(你用Xcode編譯安裝的就是開發(fā)環(huán)境,用二維碼或者App Store下載的就是生產(chǎn)環(huán)境.)

在創(chuàng)建證書期間,需要你上傳一個(gè)證書,

打開鑰匙來制作這個(gè)證書
打開MAC自帶的鑰匙串訪問(Launchpad->其他->鑰匙串訪問)
點(diǎn)開后左上角打開證書助手,從證書頒發(fā)機(jī)構(gòu)請求證書

注意:要存儲(chǔ)到磁盤

然后選擇存儲(chǔ)路徑
這時(shí)候?qū)⒅谱鞯淖C書上傳了

下載下來,點(diǎn)擊Done,又回跳回到開始的界面,然后還是選擇你創(chuàng)建的App ID然后Edit,開發(fā)環(huán)境和生產(chǎn)環(huán)境推送證書的創(chuàng)建流程是一樣的,自己按著步驟就能把證書全部創(chuàng)建并下載成功.

這時(shí)候Push Notifications應(yīng)該是綠的了

打開鑰匙串訪問,你會(huì)發(fā)現(xiàn)你多了這兩個(gè)證書

右鍵分別導(dǎo)出它們

設(shè)置密碼(這里的密碼將會(huì)在極光推送管理平臺(tái)上進(jìn)行驗(yàn)證?)

導(dǎo)出成功后,命名最好用英文名
二.極光推送開發(fā)者身份
ok,證書的問題先告一段落了,下一步,注冊極光推送的開發(fā)者賬號
如果你沒有注冊,先在極光官網(wǎng)進(jìn)行注冊
創(chuàng)建一個(gè)應(yīng)用
在應(yīng)用配置中導(dǎo)入兩個(gè)證書(我這是已經(jīng)驗(yàn)證了的,不然就是讓你上傳證書的按鈕)
Bundle ID是導(dǎo)入證書后自動(dòng)出現(xiàn)的,證書最好一個(gè)一個(gè)上傳不然可能會(huì)出現(xiàn)驗(yàn)證失敗的問題.

然后下載SDK
三.工程
把SDK中的Lib文件夾導(dǎo)入你的項(xiàng)目中,記得勾選Copy

導(dǎo)入相關(guān)庫
在Build Phases中導(dǎo)入以下庫
----Jpush-iOS10的問題

修改Capabilities
打開遠(yuǎn)程推送

打開Background Modes

修改Bundle ID 以及選擇Team(推送無法在模擬機(jī)上測試)

四.代碼
ok,環(huán)境搞定,開始代碼
在代理的.h中
staticNSString*appKey =@"2dd13edbb353c542440ab572";
staticNSString*channel =@"Publish channel";
staticBOOLisProduction =FALSE;

h
代碼1:
在代理的.m中
記得必須添加<UserNotifications/UserNotifications.h>,否則無法監(jiān)聽
/**
自己的信息填寫
*/
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//??? self.window.backgroundColor = [UIColor whiteColor];
//??? [self.window makeKeyAndVisible];
if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {
[JPUSHServiceregisterForRemoteNotificationTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:nil];
}else{
[JPUSHServiceregisterForRemoteNotificationTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:nil];
}
[JPUSHServicesetupWithOption:launchOptionsappKey:appKeychannel:channelapsForProduction:NO];
returnYES;
}
/**
Token方法中注冊設(shè)備
*/
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
// Required
[JPUSHServiceregisterDeviceToken:deviceToken];
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHServicehandleRemoteNotification:userInfo];
}
/**
:App在后臺(tái)時(shí)收到推送時(shí)的處理
*/
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
/**
創(chuàng)建didFailToRegisterForRemoteNotificationsWithError方法,處理接收推送錯(cuò)誤的情況(一般不會(huì)…)
*/
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
/**
在applicationWillEnterForeground方法(App即將進(jìn)入前臺(tái))中將小紅點(diǎn)清除
*/
- (void)applicationWillEnterForeground:(UIApplication*)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
NSLog(@"進(jìn)入前臺(tái)");
[UIApplicationsharedApplication].applicationIconBadgeNumber=0;
}
代碼2:
/**
自己的信息填寫
*/
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
if([[UIDevicecurrentDevice].systemVersionfloatValue] >=10.0) {
JPUSHRegisterEntity* entity = [[JPUSHRegisterEntityalloc]init];
entity.types=UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHServiceregisterForRemoteNotificationConfig:entitydelegate:self];
}
elseif([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {
//可以添加自定義categories
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge|
UIUserNotificationTypeSound|
UIUserNotificationTypeAlert)
categories:nil];
}
else{
//categories必須為nil
[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|
UIRemoteNotificationTypeSound|
UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHServicesetupWithOption:launchOptionsappKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:nil];//這里是沒有advertisingIdentifier的情況,有的話,大家在自行添加
//注冊遠(yuǎn)端消息通知獲取device token
[applicationregisterForRemoteNotifications];
returnYES;
}
/**
Token方法中注冊設(shè)備
*/
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
// Required
[JPUSHServiceregisterDeviceToken:deviceToken];
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHServicehandleRemoteNotification:userInfo];
}
// ios 10 support處于前臺(tái)時(shí)接收到通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(NSInteger))completionHandler
{
NSDictionary* userInfo = notification.request.content.userInfo;
if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
[JPUSHServicehandleRemoteNotification:userInfo];
//添加各種需求。。。。。
}
completionHandler(UNNotificationPresentationOptionAlert);
//處于前臺(tái)時(shí),添加需求,一般是彈出alert跟用戶進(jìn)行交互,這時(shí)候completionHandler(UNNotificationPresentationOptionAlert)這句話就可以注釋掉了,這句話是系統(tǒng)的alert,顯示在app的頂部,
}
/**
:App在后臺(tái)時(shí)收到推送時(shí)的處理
*/
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
/**
創(chuàng)建didFailToRegisterForRemoteNotificationsWithError方法,處理接收推送錯(cuò)誤的情況(一般不會(huì)…)
*/
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
/**
在applicationWillEnterForeground方法(App即將進(jìn)入前臺(tái))中將小紅點(diǎn)清除
*/
- (void)applicationWillEnterForeground:(UIApplication*)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
NSLog(@"進(jìn)入前臺(tái)");
[applicationsetApplicationIconBadgeNumber:0];
[applicationcancelAllLocalNotifications];
}
遇到的一個(gè)問題:
Undefined symbols for architecture arm64:
"_dns_parse_resource_record", referenced from:
-[JPUSHSRVResolver processRecord:length:] in jpush-ios-2.2.0.a(JPUSHSRVResolver.o)
"_dns_free_resource_record", referenced from:
-[JPUSHSRVResolver processRecord:length:] in jpush-ios-2.2.0.a(JPUSHSRVResolver.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
解決方案:
添加libresolv.tbd庫,即可解決問題
效果:


