iOS推送(利用極光推送)

本文主要是基於極光推送的SDK封裝的一個快速集成極光推送的類的封裝(不喜勿噴)ios

 

(1)首先說一下推送的一些原理:web

Push的原理:服務器

Push 的工做機制能夠簡單的歸納爲下圖
app

圖中,Provider是指某個iPhone軟件的Push服務器,這篇文章我將使用.net做爲Provider。 
APNS 是Apple Push Notification Service(Apple Push服務器)的縮寫,是蘋果的服務器。框架

上圖能夠分爲三個階段。less

第一階段:.net應用程序把要發送的消息、目的iPhone的標識打包,發給APNS。 
第二階段:APNS在自身的已註冊Push服務的iPhone列表中,查找有相應標識的iPhone,並把消息發到iPhone。 
第三階段:iPhone把發來的消息傳遞給相應的應用程序, 而且按照設定彈出Push通知。ide

 

    從上圖咱們能夠看到。fetch

   一、首先是應用程序註冊消息推送。網站

   二、 IOS跟APNS Server要deviceToken。應用程序接受deviceToken。ui

   三、應用程序將deviceToken發送給PUSH服務端程序。

   四、 服務端程序向APNS服務發送消息。

   五、APNS服務將消息發送給iPhone應用程序。

    不管是iPhone客戶端跟APNS,仍是Provider和APNS都須要經過證書進行鏈接的。下面我介紹一下幾種用到的證書。

 

(2)關於基於極光推送SDK類的封裝


  2.1  建立一個繼承於NSObject的類  ,暫時命名爲  MyJPush吧

  在.h文件中先添加幾個方法:

  /** 註冊JPush */

  +(void)registerJPush:(NSDictionary *)launchOptions;

 

  /** 添加監聽者 */

  +(void)addJPushListener:(id<CoreJPushProtocol>)listener;

 

  /** 移除監聽者 */

  +(void)removeJPushListener:(id<CoreJPushProtocol>)listener;

 

  /** 註冊alias、tags */

  +(void)setTags:(NSSet *)tags alias:(NSString *)alias resBlock:(void(^)(BOOL res, NSSet *tags,NSString *alias))resBlock;

 

  .m文件的實現:

 

 須要宏定義一些變量:

   

  #define JPushAppKey @"***********"   //極光推送的APPKey

  #define JPushChannel @"AppStore"         //指明應用程序包的下載渠道

  #define JPushIsProduction NO                 //是不是生產環境

  

  /** 註冊JPush */

  +(void)registerJPush:(NSDictionary *)launchOptions{

    

    // Required

    //能夠添加自定義categories

    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |

                                                      UIUserNotificationTypeSound |

                                                      UIUserNotificationTypeAlert)

                                          categories:nil];

    

    // Required

    //如需兼容舊版本的方式,請依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化和同時使用pushConfig.plist文件聲明appKey等配      置內容。

    [JPUSHService setupWithOption:launchOptions appKey:JPushAppKey channel:JPushChannel apsForProduction:JPushIsProduction];

    

}

 

 

 

  /** 添加監聽者 */

  +(void)addJPushListener:(id<CoreJPushProtocol>)listener{

    

    MyJPush *jpush = [MyJPush sharedCoreJPush];

    

    if([jpush.listenerM containsObject:listener]) return;

    

    [jpush.listenerM addObject:listener];

}

 

 

  /** 移除監聽者 */

  +(void)removeJPushListener:(id<CoreJPushProtocol>)listener{

    

    MyJPush *jpush = [MyJPush sharedCoreJPush];

    

    if(![jpush.listenerM containsObject:listener]) return;

    

    [jpush.listenerM removeObject:listener];

}

 

 

  -(NSMutableArray *)listenerM{

    

    if(_listenerM==nil){

        _listenerM = [NSMutableArray array];

    }

    

    return _listenerM;

}

 

 

  -(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{

    

    [self handleBadge:[userInfo[@"aps"][@"badge"] integerValue]];

    

    if(self.listenerM.count==0) return;

    

    [self.listenerM enumerateObjectsUsingBlock:^(id<CoreJPushProtocol> listener, NSUInteger idx, BOOL *stop) {

        

        if([listener respondsToSelector:@selector(didReceiveRemoteNotification:)]) [listener didReceiveRemoteNotification:userInfo];

    }];

}

 

 

 

  /** 處理badge */

  -(void)handleBadge:(NSInteger)badge{

    

    NSInteger now = badge-1;

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    [UIApplication sharedApplication].applicationIconBadgeNumber=0;

    [UIApplication sharedApplication].applicationIconBadgeNumber=now;

    [JPUSHService setBadge:now];

}

 

 

 

  +(void)setTags:(NSSet *)tags alias:(NSString *)alias resBlock:(void(^)(BOOL res, NSSet *tags,NSString *alias))resBlock{

    

    MyJPush *jpush = [MyJPush sharedCoreJPush];

 

    [JPUSHService setTags:tags alias:alias callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:jpush];

    

    jpush.ResBlock=resBlock;

}

 

 

  -(void)tagsAliasCallback:(int)iResCode tags:(NSSet *)tags alias:(NSString *)alias{

 

    if(self.ResBlock != nil) self.ResBlock(iResCode==0,tags,alias);

}

 

  2.2  其次建立一個基於APPDelegate的類別文件     暫時命名爲  AppDelegate+JPush

 

  實現下列幾個方法:

  

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    

    // Required

    [JPUSHService registerDeviceToken:deviceToken];

}

 

  - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    

    // Required,For systems with less than or equal to iOS6

    [JPUSHService handleRemoteNotification:userInfo];

}

 

  - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {  

 

    // IOS 7 Support Required

    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

    

    CoreJPush *jpush = [CoreJPush sharedCoreJPush];

    [jpush didReceiveRemoteNotification:userInfo];

}

 

這兩個文件能夠直接拿到本身的項目中使用,而後在項目中添加如下須要依賴的庫

.CFNetwork.framework

.CoreFoundation.framework

.CoreTelephony.framework

.SystemConfiguration.framework

.Security.framework

. libz.tbd

 

第二步進行項目配置:

. (1) Search Paths 下的 User Header Search Paths 和 Library Search Paths爲`$(PROJECT_DIR)/CoreJPush/CoreJPush/Lib`。

. (2) 選中Project-Target-Capabilities-Background Modes,勾選Remote Notifications。

. (3) 請修改CoreJPush框架內Common文件夾下PushConfig.plist的Appkey爲您的Appkey。

. (4) 若是你的工程須要支持小於7.0的iOS系統,請到Build Settings 關閉 bitCode 選項,不然將沒法正常編譯經過。

. (5)容許XCode7支持Http傳輸方法

        

        若是用的是Xcode7時,須要在App項目的plist手動加入如下key和值以支持http傳輸:

        

          <key>NSAppTransportSecurity</key> 

              <dict> 

          <key>NSAllowsArbitraryLoads</key> 

                <true/> 

            </dict>

 

最重要的一步:

   1.註冊JPush

   請刪除您的AppDelgate中全部有關推送的方法,由於CoreJPush內部已經封裝。

 

    #import "MyJPush.h"

    //註冊JPush

    [MyJPush registerJPush:launchOptions];

  

2.在您任意想獲得推送數據的地方,三句代碼搞定:

 

      //1.添加一個監聽者:此監聽者是遵循了CoreJPushProtocol協議

      [MyJPush addJPushListener:self];

      

      

      //2.你須要在合適的地方(好比dealloc),移除監聽者

      [MyJPush removeJPushListener:self];

      

      

      //3.您已經遵循了MyJPushProtocol協議,直接在.m文件裏面敲did ,Xcode會提示你以下方法:

      -(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{

          NSLog(@"ViewController: %@",userInfo);

      }

 

    #pragma mark   -   在極光推送網站發送消息,帶參數,手機接收到消息後進行一系列操做

 

    //獲取推送的信息(包括推送時添加的key和value,經過key獲取value的值)

    -(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{

    NSLog(@"controller: %@",userInfo);

    

    if ([userInfo.allKeys containsObject :@"key"]) {           //這裏的key是本身在極光推送的平臺上發送通知時本身建立的key和value

        NSLog(@"發送通知成功,開始跳轉");

        

        WebViewController *webVc = [[WebViewController alloc]init];

        

        webVc.UrlString = userInfo[@"key"];

        

        [self.navigationController pushViewController:webVc animated:YES];

        

    }

}

 

別的一些操做查看極光推送的文檔就能夠了,封裝兩個文件是爲了之後配置和使用的時候更加方便,還有比較簡單的證書的配置啊什麼的就再也不多說了,網上以及極光推送官網的文檔中有很明確的說明,此處須要使用的是極光推送 jpush-ios-2.1.5之後的版本

 

有須要或者指正的小夥伴能夠給我留言哦!

如需源碼,可留言單獨贈送

相關文章
相關標籤/搜索