第一步 先在開發者賬號創建app id,即創建應用程序ID
詳細步驟:
1、登陸?蘋果開發者網站?進入開發者賬戶。
從開發者賬戶頁面左側入口進入“Certificates, IDs & Profiles” 頁面。
2、創建 App ID,填寫 App ID 的 Name 和 Bundle ID(? ?注: 此處需要指定具體的 Bundle ID 不要使用通配符)。
3、為 App 開啟 Push Notification 功能。如果是已經創建的 App ID 也可以通過設置開啟 Push Notification 功能。
4、填寫好以上屬性后,點擊 “Continue”,確認 AppId 屬性的正確性,點擊 “Register”,注冊 AppId 成功。
第二步 配置證書,包括開發證書和生產證書兩個證書
簡述極光推送證書的基本生成流程,兩者都為導出的p12文件
開發證書的詳細步驟:
1、開發證書的生成(注意選擇的Development)。
2、點擊continue,來到選擇App ID界面。
3、說明讓上傳自己的CSR文件,continue...
4、上傳請求生成的CSR文件,continue...
5、打開系統自帶的 “鑰匙串訪問” 創建 Certificate Signing Request。如下圖操作:
6、填寫“用戶郵箱”和“常用名稱” ,并選擇“存儲到磁盤”,證書文件后綴為.certSigningRequest 。
7、回到瀏覽器中 CSR 上傳頁面,上傳剛剛生成的后綴為 .certSigningRequest 的文件。
8、生成證書成功后,點擊 “Download” 按鈕把證書下載下來,是后綴為 .cer 的文件(第一個為“Download”得到的生產開發證書,第二個為“Download”得到的開發證書)。
9、雙擊證書后,會在“KeychainAccess”中打開,選擇左側“鑰匙串”列表中“登錄”,以及“種類”列表中“我的證書”,找到剛才下載的證書,并導出為 .p12 文件。如下圖:
10、最后得到導出的證書(第二個)
生產證書的詳細步驟:
1、注意這里的選擇方式和開發證書有略微的區別,選擇Production那一項就好 。
2、以下的步驟略去,和開發的一樣,到最后下載一下文件,得到下面圖的兩個文件,雙擊在鑰匙串訪問中可見。
3、生產和開發cer文件
4、分別導出的開發證書和生產證書到桌面上(雙擊證書后,會在“KeychainAccess”中打開,選擇左側“鑰匙串”列表中“登錄”,以及“種類”列表中“我的證書”,找到剛才下載的證書,并導出為 .p12 文件)。如下圖:
第三步?把導出的開發證書和生產證書分別上傳到極光平臺
1、在極光控制臺上,進入你應用的應用設置中 iOS 的鑒權方式選擇 “證書”,上傳剛才導出的 .p12 證書。極光會在后臺為你的應用進行鑒權。
第四步 集成極光推送SDK,配置工程
1、導入SDK
選擇1:Cocoapods導入
通過Cocoapods下載地址:
pod 'JPush'
如果需要安裝指定版本則使用以下方式(以3.0.2版本為例):
pod 'JPush', '3.0.2'
選擇2:手動導入
在極光官網下載最新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 (Xcode7以下版本是libz.dylib)、AdSupport.framework (獲取IDFA需要;如果不使用IDFA,請不要添加)、UserNotifications.framework (Xcode8及以上)、libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)(注意:?
如果集成JPush 3.0.1及以上版本, 且同時集成極光其他SDK(如:JMessage 3.0.0及以上版本) 1. Cocoapods導入,建議都更新為線上最新版本,來避免Jcore版本不一致導致的沖突。 2. 手動導入,在工程中只需保留一個最新版本的jcore-ios-x.x.x.a靜態庫文件。)
3090071-8c949b2daaa8f044.png
Build Settings
如果你的工程需要支持小于7.0的iOS系統,請到Build Settings 關閉 bitCode 選項,否則將無法正常編譯通過。
設置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夾(默認為lib)與工程文件在同一級目錄下,則都設置為"$(SRCROOT)/{靜態庫所在文件夾名稱}"即可。
Capabilities
如使用Xcode8及以上環境開發,請開啟Application Target的Capabilities->Push Notifications選項,如圖:?
允許Xcode7支持Http傳輸方法
如果您使用的是2.1.9及以上的版本則不需要配置此步驟 如果用的是Xcode7或更新版本,需要在App項目的plist手動配置下key和值以支持http傳輸:
選擇1:根據域名配置
在項目的info.plist中添加一個Key:NSAppTransportSecurity,類型為字典類型。
然后給它添加一個NSExceptionDomains,類型為字典類型;
把需要的支持的域添加給NSExceptionDomains。其中jpush.cn作為Key,類型為字典類型。
每個域下面需要設置2個屬性:NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads。 兩個屬性均為Boolean類型,值分別為YES、YES。
如圖:
選擇2:全局配置
第五步 工程代碼部分
1、 進入項目的appdelegate里面,首先導入頭文件和遵循代理
import "AppDelegate.h"
import "JPUSHService.h"
ifdef NSFoundationVersionNumber_iOS_9_x_Max
import
@interface AppDelegate ()
2、? 在didFinishLaunchingWithOptions方法中配置
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self jpushInitWith:launchOptions];
}
-(void)jpushInitWith:(NSDictionary)launchOptions
{
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types =? ? JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//生成idfa 字段
NSString
advertisingId=[SimulateIDFA createSimulateIDFA];
DDLog(@"----ifda----%@",advertisingId);
if (advertisingId) {
[[NSUserDefaults standardUserDefaults] setObject:advertisingId forKey:@"advertisingId"];
}
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kJPFNetworkDidLoginNotification object:nil];
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:advertisingId];
//設置紅色角標
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[JPUSHService setBadge:0];
}
3、? 實現通知和協議方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//發送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"showRedBadge" object:nil userInfo:userInfo];
// Required, iOS 7 Support[JPUSHService handleRemoteNotification:userInfo];? ? completionHandler(UIBackgroundFetchResultNewData);? }? - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {? ? ? DDLog(@"----userinfo---%@----",userInfo);// Required,For systems with less than or equal to iOS6[JPUSHService handleRemoteNotification:userInfo];? }? - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {//OptionalNSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);? ? }? ? - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken? {? ? ? ? [JPUSHService registerDeviceToken:deviceToken];? }#pragma mark- JPUSHRegisterDelegate// iOS 10 Support- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void(^)(NSInteger))completionHandler {// RequiredNSDictionary* userInfo = notification.request.content.userInfo;if([notification.request.trigger isKindOfClass:[UNPushNotificationTriggerclass]])? ? ? ? {? ? ? [JPUSHService handleRemoteNotification:userInfo];? }? ? completionHandler(UNNotificationPresentationOptionAlert);// 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置}// iOS 10 Support- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {// RequiredNSDictionary* userInfo = response.notification.request.content.userInfo;if([response.notification.request.trigger isKindOfClass:? [UNPushNotificationTriggerclass]]) {? ? [JPUSHService handleRemoteNotification:userInfo];? ? }? ? completionHandler();// 系統要求執行這個方法}
4、pragma mark? 通知
- (void)networkDidReceiveMessage:(NSNotification*)notification {NSDictionary* userInfo = [notification userInfo];// NSString *content = [userInfo valueForKey:@"content"];// NSDictionary *extras = [userInfo valueForKey:@"extras"];// NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服務端傳遞的Extras附加字段,key是自己定義的DDLog(@"----userInfo---%@",userInfo);//發送通知[[NSNotificationCenterdefaultCenter] postNotificationName:@"showRedBadge"object:niluserInfo:userInfo];} - (void)networkDidLogin:(NSNotification*)notification? {NSLog(@"已登錄");if([JPUSHService registrationID]) {//下面是我拿到registeID,發送給服務器的代碼,可以根據你需求來處理NSString*registerid = [JPUSHService registrationID];NSLog(@"APPDelegate開始上傳rgeisterID---%@",registerid);if(registerid)? ? {? ? ? [[NSUserDefaultsstandardUserDefaults] setObject:registerid forKey:@"registerid"];? ? }? ? ? ? ? }? }
第六步 推送測試
相關文檔可點擊以下鏈接:
1、https://blog.csdn.net/zhanglizhi111/article/details/79507549
2、https://blog.csdn.net/lqq200912408/article/details/54376208
3、https://blog.csdn.net/cc1991_/article/details/78782946
[iOS真機測試流程(完整版)](http://www.lxweimin.com/p/ae7110f2f7d9)