外面電閃雷鳴,看了下時間,已經(jīng)到11點了,辦公室內(nèi)一片繁忙,沒有嘈雜的聊天聲音,只有“啪啪啪”的鍵盤聲音,此時此刻我正在瘋狂的調(diào)試著bug,心里不停的在吐槽,這手機推送到底要怎么搞,再弄不出來,老板就要炒我魷魚了,想到這里,心里一陣委屈,我這么拼,之前瘋狂碼代碼的努力難道要白費了嗎,難道要死在這個推送上嗎?。。?br> 突然瀏覽器的右下角彈出了一個廣告:極光推送,解放你的雙手!萬念俱灰的我抱著一絲僥幸心理點開了廣告,跳轉(zhuǎn)到了一個官網(wǎng),但是沒想到這個廣告竟然改變了我的命運!

極光
PS:此時此刻我的心理樂開了花,充滿期待又充滿驚恐,怕這個是騙人的
試一試吧,于是我查看文檔,此時此刻我的疑惑已經(jīng)煙消云散,xxx為啥極光的廣告這么少,害的我差點準備跑路,極光官網(wǎng)
一、注冊
二、點擊極光推送的文檔
三、集成步驟

集成步驟
1. 這個時候心里已經(jīng)吃了一顆定心丸了,不怕被炒魷魚了,在導入之前需要配置幾個證書,這個對于帥氣的我來說不在話下,三下兩下就配好了,在這里具體的步驟可以參考極光推送證書配置指南,這里寫的比我詳細多了,感謝極光工作人員的認真細致,不然我又要花大把的時間在配置證書上,么么噠??
PS:此時此刻已經(jīng)11.30了,不過我不怕,有極光在,心里很安心
2. 導入步驟,這個時候心里越來越信任極光了
- Cocoapods 導入
pod 'JPush', '3.1.0'
- 手動導入(比較麻煩,不是很推薦)
- 在極光官網(wǎng)下載最新 SDK
- 將 SDK 包解壓,在 Xcode 中選擇 “Add files to 'Your project name'...”,將解壓后的 lib 子文件夾(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a )添加到你的工程目錄中。
- 添加 Framework
- CFNetwork.framework
- CoreFoundation.framework
- CoreTelephony.framework
- SystemConfiguration.framework
- CoreGraphics.framework
- Foundation.framework
- UIKit.framework
- Security.framework
- libz.tbd
- UserNotifications.framework
- libresolv.tbd
注意: 如果集成 JPush 3.0.1 及以上版本, 且同時集成極光其他 SDK(如:JMessage 3.0.0 及以上版本)
- Cocoapods 導入,建議都更新為線上最新版本,來避免 JCore 版本不一致導致的沖突
- 手動導入,在工程中只需保留一個最新版本的 jcore-ios-x.x.x.a 靜態(tài)庫文件。
-
開啟Target 的 Capabilities->Push Notifications 選項,如圖:
集成步驟 - 在AppDelegate.m里引入
// 引入 JPush 功能所需頭文件
#import "JPUSHService.h"
// iOS10 注冊 APNs 所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
-
初始化請將以下代碼添加到
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里,JpushAppKey是你申請的key,這邊是大家經(jīng)常會犯錯的地方,apsForProduction:NO表示采用的是開發(fā)證書,YES表示采用生產(chǎn)證書發(fā)布應用
//初始化APNs
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//初始化JPush
#ifdef DEBUG
[JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:NO];
#else
[JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:YES];
#endif
- 為 AppDelegate 添加 Delegate(JPUSHRegisterDelegate) 并添加回調(diào)方法
@interface AppDelegate ()<JPUSHRegisterDelegate>
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required - 注冊 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
@end
- 添加處理 APNs 通知回調(diào)方法
#pragma mark- JPUSHRegisterDelegate
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//從通知界面直接進入應用
}else{
//從通知設置界面進入應用
}
}
// 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í)行這個方法,選擇是否提醒用戶,有 Badge、Sound、Alert 三種類型可以選擇設置
}
// 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í)行這個方法
}
- (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];
}
- 上傳registrationID給服務器端(也可以設置別名,設置別名也在這里寫)
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
XBLog(@"------>registrationID: %@",[JPUSHService registrationID]);
//上傳極光registrationID給服務器
}];
PS:已經(jīng)12點了,這個時候萬事俱備只欠東風,來cmd+R,心里的僥幸心理又出現(xiàn)了,假如不行怎么辦,別怕,極光給出了方案
常見收不到通知的原因
- 環(huán)境不對,先檢查客戶端的方法
- 初始化代碼中 apsForProduction:isProduction 的值需要與客戶端的證書環(huán)境保持一致
- 后臺Api推送的話再去看后臺的,后臺也有一個配置生產(chǎn)還是測試環(huán)境的屬性.
- 前后端的appkey不一致
此時此刻見證奇跡的時刻到了,為了驗證集成到底有沒有正確,請看下邊

測試
