iOS推送(利用极光推送)

本文主要是基于极光推送的SDK封装的一个快速集成极光推送的类的封装(不喜勿喷)

 

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

Push的原理:

Push 的工作机制可以简单的概括为下图

图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider。 
APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器。

上图可以分为三个阶段。

第一阶段:.net应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。 
第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。 
第三阶段:iPhone把发来的消息传递给相应的应用程序, 并且按照设定弹出Push通知。

 

    从上图我们可以看到。

   1、首先是应用程序注册消息推送。

   2、 IOS跟APNS Server要deviceToken。应用程序接受deviceToken。

   3、应用程序将deviceToken发送给PUSH服务端程序。

   4、 服务端程序向APNS服务发送消息。

   5、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以后的版本

 

有需要或者指正的小伙伴可以给我留言哦!

如需源码,可留言单独赠送

 

posted @ 2016-08-05 15:54  Mr.pengge  阅读(4846)  评论(3编辑  收藏  举报