iOS: 極光推送

以前作過環信和友盟的推送,照着官方文檔集成其實挺簡單的,今天公司須要,特意作了一下極光推送。不用不知道,原來極光推送集成如此簡單,不得不說說了。html

固然作推送錢須要作一些準備工做了,就是推送必須的p12推送證書:開發環境(開發時測試須要的推送證書)、生產環境(發佈到AppStore時須要的推送證書),由於xcode已經升級到了7.0以上,因此一些真機測試的配置文件證書就不須要本身手動去建立了,只要有Apple ID,真機測試時,就能自動生成,免費測試:ios

製做證書的過程就不囉嗦了,詳細看官方文檔或者以下推薦:xcode

http://jingyan.baidu.com/article/c1465413975cba0bfcfc4ccf.html服務器

http://docs.jpush.io/client/ios_tutorials/#ios_1app

http://docs.jpush.io/guideline/ios_guide/框架

http://allluckly.cn/投稿/tuogao28?utm_source=tuicool&utm_medium=referralless

建立完證書,就是去極光官網註冊帳號,建立應用,截圖以下:ide

將建立的證書上傳到應用上了,上傳成功後的截圖以下:工具

證書上傳成功後,生成APP Key,截圖以下:測試

好了,這下工做作完了,剩下的就是代碼實現了:

第一步:下載SDK,將須要的兩個文件導入項目中:

集成壓縮包內容

包名爲JPush-iOS-SDK-{版本號}

  • lib文件夾:包含頭文件 JPUSHService.h,靜態庫文件jpush-ios-x.x.x.a ,支持的iOS版本爲 5.0 及以上版本。(請注意:模擬器不支持APNs)
  • pdf文件:集成指南
  • demo文件夾:示例

第二步:導入須要依賴的庫文件:

必要的框架

  • 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,請不要添加)

第三步:建立一個工具類,名稱爲KJJPushHelper,封裝註冊時的各類方法

.h

//
// KJJPushHelper.h //
// Created by xiayuanquan on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // 
#import <Foundation/Foundation.h>

@interface KJJPushHelper : NSObject // 在應用啓動的時候調用
+ (void)setupWithOption:(NSDictionary *)launchingOption appKey:(NSString *)appKey channel:(NSString *)channel apsForProduction:(BOOL)isProduction advertisingIdentifier:(NSString *)advertisingId; // 在appdelegate註冊設備處調用
+ (void)registerDeviceToken:(NSData *)deviceToken; // ios7之後,纔有completion,不然傳nil
+ (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion; // 顯示本地通知在最前面
+ (void)showLocalNotificationAtFront:(UILocalNotification *)notification; @end

.m

//
// KJJPushHelper.m // Created by xiayuanquan on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // 
#import "KJJPushHelper.h"
#import "JPUSHService.h"

@implementation KJJPushHelper + (void)setupWithOption:(NSDictionary *)launchingOption appKey:(NSString *)appKey channel:(NSString *)channel apsForProduction:(BOOL)isProduction advertisingIdentifier:(NSString *)advertisingId{ // Required
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    // ios8以後能夠自定義category
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // 能夠添加自定義categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
        // ios8以前 categories 必須爲nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; #endif } #else
    // categories 必須爲nil
    [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; #endif
    
    // Required
 [JPUSHService setupWithOption:launchingOption appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId]; return; } + (void)registerDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; return; } + (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion { [JPUSHService handleRemoteNotification:userInfo]; if (completion) { completion(UIBackgroundFetchResultNewData); } return; } + (void)showLocalNotificationAtFront:(UILocalNotification *)notification { [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil]; return; } @end

第四步:建立一個APPDelegate的分類,在該類中調用KJJPushHelper中的類方法

// AppDelegate+KJJPushSDK.h //
// Created by xiayuanquan on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // 
#import "AppDelegate.h"

@interface AppDelegate (KJJPushSDK) -(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; @end



// AppDelegate+KJJPushSDK.m //
// Created by xiayuanquan on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // 
#import "AppDelegate+KJJPushSDK.h"
#import "KJJPushHelper.h"

#define JPushSDK_AppKey  @"31e01f6a2f6dxxxxxxxxxec"
#define isProduction     NO

@implementation AppDelegate (KJJPushSDK) -(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [KJJPushHelper setupWithOption:launchOptions appKey:JPushSDK_AppKey channel:nil apsForProduction:isProduction advertisingIdentifier:nil]; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Required - 註冊 DeviceToken
 [KJJPushHelper registerDeviceToken:deviceToken]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required,For systems with less than or equal to iOS6
 [KJJPushHelper handleRemoteNotification:userInfo completion:nil]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // IOS 7 Support Required
 [KJJPushHelper handleRemoteNotification:userInfo completion:completionHandler]; // 應用正處理前臺狀態下,不會收到推送消息,所以在此處須要額外處理一下
    if (application.applicationState == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息" message:userInfo[@"aps"][@"alert"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"肯定",nil]; [alert show]; } } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [KJJPushHelper showLocalNotificationAtFront:notification]; return; } - (void)applicationDidBecomeActive:(UIApplication *)application { [application setApplicationIconBadgeNumber:0]; return; } @end

第五步:在AppDelegate中註冊便可

//註冊極光推送
[self JPushApplication:application didFinishLaunchingWithOptions:launchOptions];

好了,大功告成,插上真機運行:打印結果以下

去官網測試一下:

真機收到消息截圖:

集成過程當中遇到的問題,困擾了很久,後來找出來了,分享一下:

當時證書一切都沒有問題,可是老是出現這個打印:

錯誤信息JPUSH | W - [JPUSHClientController] Not get deviceToken yet. Maybe: your certificate not configured APNs? or current network is not so good so APNs registration failed? or there is no APNs register code? Please refer to JPush docs.

推送消息時,出現的提示:

 

個人緣由是:

因爲項目以前用到了環信SDK,環信得已經註冊了通知,在AppDelegate中註冊通知,didRegisterForRemoteNotificationsWithDeviceToken與didFailToRegisterForRemoteNotificationsWithError方法,均不執行。。。需到環信註冊通知的地方,再次註冊極光通知。方能夠獲取到Token執行。

 

擴展:極光推送中的定向推送

極光推送中,不使用廣播推送,那麼怎樣作到定向推送,是開發者和需求必定會出現的問題,極光推送中能夠有兩個惟一值:

(1)註冊Jpush成功後生成的registrationID,這個registrationID是標記設備惟一性的,你發現,當你在啓動屢次,註冊Jpush時,這個值是不變的;在同一個設備上,更換用戶登陸,這個值仍然不變;最後,你刪除應用程序,再下載時啓動註冊Jpush,這個值仍是不變。這就能夠定向向某臺設備作推送,若是你能給本身的服務器上傳這個值,而且給這個值綁定一些東西,是否是能夠作更多事情呢。

(2)alias:只要瞭解極光推送的都知道這是設置別名的,官方文檔上說明了這個值不是惟一的,可是建議開發者把它做爲用戶的惟一標記。我以爲這個做爲惟一值是最好的,當你想定向向某個用戶作推送,或者召喚他迴歸咱們的應用程序,這個值就太好了。你能夠將它設置爲userId,這個時候推送就能知道向哪一個用戶發了。

 

本人原創,轉載須註明出處,謝謝!

相關文章
相關標籤/搜索