极光推送

作者: YvanLiu | 来源:发表于2017-06-14 15:01 被阅读266次

网上能找到一堆关于推送的文章,前几天接手其他同事的代码,加功能顺便梳理一下,感觉想要的在官方文档里面都能找到,写的很详细。

极光文档
SDK集成和证书的设置这里不弄了,文档里写的很详细,这里只记录一下相关代码部分。

极光有两种发送消息的机制:自定义消息和通知

  • 自定义消息:
    没有类似通知的横幅声音等,可自定义发送过来的数据格式以及字段。
    而且只有在前端运行的时候才能收到自定义消息的推送。
  • 通知:
    就如我们平时用的通知,格式固定,但也可加扩展字段。

注册

  • 位置
#import "AppDelegate.h"中的 - (BOOL)application: didFinishLaunchingWithOptions: 
如果AppDelegate中代码较多,建议创建一个分类如: AppDelegate+JPush 
在AppDelegate的didFinishLaunchingWithOptions方法中调用,更清晰。
  • 头文件
#import "JPUSHService.h"
#import <UserNotifications/UserNotifications.h>
添加代理 <JPUSHRegisterDelegate>
  • 代码
// 初始化APNs代码
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

    } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
    } else {

        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
    }
  NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

 // 初始化JPush代码
 [JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];

 //JPush标识此设备的 registrationID。
 [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功:%@",registrationID);   
        } else{
            NSLog(@"registrationID获取失败,code:%d",resCode);
        }
    }];

现在iOS11都马上出来了,估计以后也不用再适配8以下的了吧。

  • 注册APNs成功并上报DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}

  • 注册失败
#pragma mark  极光注册 失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error
          );
}

通知部分

iOS10之后,接到通知:亲测,程序在前台运行时会调用此方法。
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  {
        // iOS 10 Support
        [JPUSHService handleRemoteNotification:userInfo];
    }
    NSLog(@"-------------推送消息4----------------\n%@",userInfo);
    // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
    completionHandler(UNNotificationPresentationOptionAlert); 
}
iOS10之后,接到通知:亲测,程序在后台、杀死、点击横幅时会调用此方法。
- (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];
    }    
    NSLog(@"-------------推送消息3----------------\n%@",userInfo);
    completionHandler();
}

iOS10之后,程序在前台时也会显示横幅,所以如果有点击横幅跳制定页面的需求可以写在第二个方法中。
所以是当前台收到消息后,会直接调起第一个方法,同时弹出横幅,如果点击横幅,会调用第二个方法。

iOS7之前,没设备,没测
#pragma mark - iOS7以下 接到推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [JPUSHService handleRemoteNotification:userInfo];
    NSLog(@"-------------推送消息1----------------\n%@",userInfo);
}
iOS7之后
#pragma mark - iOS7及iOS7之后 接到推送消息后操作

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"%@",userInfo);
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    NSLog(@"-------------推送消息2----------------\n%@",userInfo);
}

如果有账号的话可以极光的这个推送试一下



这里还可以添加扩展字段,Android有专门的扩展字段获取方法,iOS就直接在收到的userInfo

{
    "_j_business" = 1;
    "_j_msgid" = 40532396844772417;
    "_j_uid" = 9734978614;
    aps =     {
        alert = "test seven 1";
        badge = 1;
        sound = default;
    };
    bizType = coupon;
}

自定义消息

自定义消息的接收比较简单,

上面已经说过,只有在前台运行时才能收到通知,但我注意到,当程序在后台或杀死的时候有自定义消息推过来,打开app后依然能够收到。

  // 监听极光推送
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
#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是自己定义的
    ······
    ······
}

也可以通过上面图一中的自定义消息方式发送消息测试。
此方法不一定要在appdelegate,可以放在你希望的位置,切记重复使用。

相关文章

  • 极光推送

    极光推送视频地址,非常详细的极光推送视频 极光推送

  • 极光推送

    极光推送 tagprivate void initJpush() {//TODO 极光推送// JPushInte...

  • 极光推送进行远程推送

    借阅:极光推送进行远程推送 怎么使用极光推送进行远程推送 在极光官网注册极光推送创建一个应用在应用配置中导入两个证...

  • ios极光推送

    第一次使用极光推送,在这里把极光推送的步骤说一下,省的以后再次用到极光推送的时候,给忘了,其实,极光推送不难...

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • 2018年功能模块沉淀

    一、推送模块 1.极光推送 文档:https://www.jiguang.cn/push备注:极光推送包括普通推送...

  • 极光推送(二)——推送的使用

    前言 在极光推送(一)——配置中讲过了极光推送的配置,这节讲讲极光推送的使用参考文档极光官网 下面以我写的demo...

  • 极光推送集成开发

    1.极光推送集成与设置 极光推送地址①注册极光推送账号。②在应用管理内按照步骤创建APP。③找到“文档——iOS—...

  • Flutter开发 集成极光推送

    Flutter推送 极光推送Flutter版本 最近研究Flutter推送,在网上找了很多资料,发现极光推送竟然有...

  • 极光征文 | 我与极光的缘分

    极光征文 | 我与极光的缘分 首先简单介绍下极光推送;极光推送(JPush)是独立的第三方云推送平台,致力于为全球...

网友评论

    本文标题:极光推送

    本文链接:https://www.haomeiwen.com/subject/yebsqxtx.html