使用JPush(極光推送)實現遠程通知

 

使用JPush(極光推送)實現遠程通知


遠程推送是APP 必備的功能, 如今第三方的 SDK 已經作的很是完備了, 在 iOS10.0出來以後, 極光推送也及時更新了他的 SDK, 今天小試了一下效果, 發現坑仍是不少的緩存

大體的思路總結一下是這樣的


使用JPush(極光推送)實現遠程通知大體思路

下面就來一步步梳理一下:

  • 一 , 建立應用程序 ID
1.首先登陸蘋果開發者網站https://developer.apple.com, 點擊 Account(若是你連蘋果開發者帳號都沒有註冊,那麼須要趕忙註冊一個)

點擊 Account
2.點擊證書選項, 紅色框框均可以點

點擊證書
3.添加一個 APP ID

Snip20161109_5.png
4.建立 APP ID

APP ID 實際上由兩部分組成, 即前綴和後綴, 前綴是默認的, 後綴就是你工程的 Bundle ID, APP ID Description就填你工程的名稱便可app


建立 APP ID - 1

勾上推送通知選項less


建立 APP ID - 2
5.點擊註冊完成建立

點擊註冊

完成建立
  • 二 , 配置和下載證書
    1.生成證書

    生成證書

    而後一路 continue, 這時候你會看到一個上傳請求
2.此時須要生成一個請求

點擊鑰匙串訪問

從證書頒發機構請求證書
3.只須要填寫用戶電子郵件地址, 我填的是我 apple id 的註冊的郵箱地址, 以及經常使用名稱兩項便可

請求證書

將證書請求文件存放到磁盤選擇桌面,就會生成這麼一個玩意兒post


證書請求文件
4.而後回到要上傳的地方, 將剛剛生成的證書文件上傳

選擇證書請求
5.下載證書, 雙擊打開

下載證書

打開後會看到這麼一個頁面學習

  • 三 , 導出 p.12文件

    導出p12文件

    導出 p.12文件,放到桌面上, 又能夠看到這麼一個玩意兒

p.12文件
  • 四 , 接下來就要生成 profile 文件了

生成 Profile 文件-1

生成 Profile 文件-2

生成 Profile 文件-3

生成 Profile 文件-4

接下來有一個設備須要註冊,這時候須要填寫你即將真機測試的 UDID測試

鏈接好你的 iPhone 到電腦, 打開 iTunes, 而後就能夠查看了fetch


查看 UDID

生成 Profile 文件-5

生成 Profile 文件-6

生成 Profile 文件時, 要填寫 ProfileName, 我填寫的是 Bundle ID - dev網站


生成 Profile 文件-7

下載 Profile 文件
  • 五 , 建立極光推送開發者帳號
1.而後在開發列表中點擊建立應用

建立應用
2.須要填寫的是應用名稱, 選取p.12文件, 填寫開發證書密碼

Snip20161109_37.png

選取p.12文件, 填寫開發證書密碼
  • 六 , 完成以後就能夠看到這個界面, 此時, 咱們就能夠開始配置 XCode環境了

應用詳情
1.下載極光推送的 SDK

下載 iOS - SDK
2.解壓後, 將 Lib 文件拖到工程裏去,包含一個靜態庫和.h 文件

iOS SDK
3.而後就是添加各類庫

CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7須要的是libz.tbd;Xcode7如下版本是libz.dylib
Adsupport.framework (獲取IDFA須要;若是不使用IDFA,請不要添加)
UserNotifications.framework(Xcode8及以上)
libresolv.tbd (JPush 2.2.0及以上版本須要)ui

添加庫完成後, 打印了這樣一句話:spa


錯誤提示

解決方案是這樣的:


解決方案
4.infoPlist文件的配置

ATS要配置
另外就是 Bundle Identifier 要修改


修改Bundle Identifier
5.添加 profile 文件到 Xcode 中,須要將自動改成手動

添加 profile 文件
6.修改 Code Signing

修改 Code Signing
  • 七 , 都配置好了之後, 就是上代碼的時刻了

    AppDelegate.m

#import "AppDelegate.h" // 引入JPush功能所需頭文件 #import "JPUSHService.h" // iOS10註冊APNs所需頭文件 #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif // 若是須要使用idfa功能所須要引入的頭文件(可選) #import <AdSupport/AdSupport.h> static NSString *const jPushTestDemoAppID = @"1b7cfe165ad7cb7c0e546405"; static NSString *const publishChannel = @"APP Store"; @interface AppDelegate ()<JPUSHRegisterDelegate> @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 註冊apns通知 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:self]; #endif } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) // iOS8, iOS9 { //能夠添加自定義categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else // iOS7 { //categories 必須爲nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; } /* * launchingOption 啓動參數. * appKey 一個JPush 應用必須的,惟一的標識. * channel 發佈渠道. 可選. * isProduction 是否生產環境. 若是爲開發狀態,設置爲 NO; 若是爲生產狀態,應改成 YES. * advertisingIdentifier 廣告標識符(IDFA) 若是不須要使用IDFA,傳nil. * 此接口必須在 App 啓動時調用, 不然 JPush SDK 將沒法正常工做. */ // 廣告標識符 NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; // 如不須要使用IDFA,advertisingIdentifier 可爲nil // 註冊極光推送 [JPUSHService setupWithOption:launchOptions appKey:jPushTestDemoAppID channel:publishChannel apsForProduction:0 advertisingIdentifier:advertisingId]; //2.1.9版本新增獲取registration id block接口。 [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) { if(resCode == 0) { // iOS10獲取registrationID放到這裏了, 能夠存到緩存裏, 用來標識用戶單獨發送推送 NSLog(@"registrationID獲取成功:%@",registrationID); [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"]; [[NSUserDefaults standardUserDefaults] synchronize]; } else { NSLog(@"registrationID獲取失敗,code:%d",resCode); } }]; return YES; } #pragma mark *** 註冊APNs成功並上報DeviceToken *** - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { /// Required - 註冊 DeviceToken [JPUSHService registerDeviceToken:deviceToken]; } #pragma mark *** 實現註冊APNs失敗接口(可選) *** - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } #pragma mark *** 添加處理APNs通知回調方法 *** #pragma mark- JPUSHRegisterDelegate // 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); // 須要執行這個方法,選擇是否提醒用戶,有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(); // 系統要求執行這個方法 } - (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 iOS6 [JPUSHService handleRemoteNotification:userInfo]; }

以上代碼都有註釋, 不少也是從官方網站上 copy 過來的, 這裏就再也不贅述了

1.運行的時候會出現

初次打開 app
2.點擊容許後, 這個時候會在控制檯打印 registrationID

獲取registrationID

是經過這個方法中的 block 回調來獲取registrationID的

[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) { if(resCode == 0) { // iOS10獲取registrationID放到這裏了, 能夠存到緩存裏, 用來標識用戶單獨發送推送 NSLog(@"registrationID獲取成功:%@",registrationID); [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"]; [[NSUserDefaults standardUserDefaults] synchronize]; } else { NSLog(@"registrationID獲取失敗,code:%d",resCode); } }];
3.在極光推送的後臺輸入你想要推送的內容

Snip20161109_56.png
4.把第二步獲取得registrationID填到這裏

Snip20161109_57.png
5.最後點擊推送便可,出現的效果就是這樣滴

遠程通知的效果

極光推送還有不少挖掘的啊, 這裏只是拋磚引玉, 你們共同窗習!

iOS開發
相關文章
相關標籤/搜索