iOS10 极光推送(远程) 小结

主要是针对iOS10不同的地方

1 首先要下载2.1.9以及Xcode8的环境 包地址  地址

2  配置Xcode8的服务




3  域名配置

3-1局部配置 这个是看极光的文档 因为2017年之后苹果强制https,所以还是要尽早换比较好


3-2 全局配置

4 需要导入的系统库

UserNotifications.framework

AdSupport.frameWork

Security.frameWork

SystemConfiguration.frameWork

CoreTelephony.frameWork

CoreFoundation.frameWork

CFNetWork.frameWork

UIKit.frameWork

Foundation.frameWork

CoreGraphics.frameWork

libz.tbd

5 导入头文件

#import "JPUSHService.h"

#import <UserNotifications/UserNotifications.h>

加入代理  JPUSHRegisterDelegate   (iOS10所需要的)

6 导入sdk之后就是要初始化极光推送的代码

我是简单的封装了一下launchOptions

#pragma mark - 创建极光推送
-(void)createJPushFromLaunchOptions:(NSDictionary *)launchOptions{
    //注册推送
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        //可以添加自定义categories
#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) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    //1 初始化
    [JPUSHService setupWithOption:launchOptions appKey:JIGUANGKEY
                          channel:@""
                 apsForProduction:APSFORPRODUCTION];
    //设置推送日志
    [JPUSHService setDebugMode];
    
    //2.1.9版本新增获取registration id block接口。
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功:%@",registrationID);
        }
        else{
            NSLog(@"registrationID获取失败,code:%d",resCode);
        }
    }];
}
其中的appKey是app申请极光的

apsForProducation 是发布环境 开发环境的时候就用NO  发布的环境就用YES 上线的时候不要忘了设置 我是用的宏定义 便于处理


然后是注册APNs并向服务器上报

//注册来自远程的推送
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //3 向服务器上报Device Token
    [JPUSHService registerDeviceToken:deviceToken];
}
注册失败的时候 这个是可选的

//注册来自远程的推送失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"error:%@",error);
}

iOS10中对于推送的响应有两种

前台得到的的通知对象

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler;

后台得到的的通知对象(当用户点击通知栏的时候)

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler;

#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#pragma mark- JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        //你的逻辑
        if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {
            //最后把Iconbadge归0
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        }
        NSLog(@"iOS10 收到远程通知:%@",userInfo);
    }
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        //你的逻辑
        if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {
            //最后把Iconbadge归0
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        }
        NSLog(@"iOS10 收到远程通知:%@",userInfo);
    }
    completionHandler(); // 系统要求执行这个方法
}
#endif

 以上就是我集成推送的过程 其中的逻辑按照需求处理即可 

其中遇到的细节

1.在iOS10 Xcode8的环境下

对于自定义推送声音的时候

需要删除当前app重新安装,才能接收到

经过网上的资料查询是因为Xcode重新覆盖安装app的时候没有加载本地的音频文件

https://forums.developer.apple.com/thread/49512

2.当注册指定用户推送的时候

[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功:%@",registrationID);
            [JPUSHService setAlias:@"12345"  callbackSelector:nil object:nil];
        }
        else{
            NSLog(@"registrationID获取失败,code:%d",resCode);
        }
    }];

因为以上的方法是异步的,所以app启动的时候不一定同步获取registrationID,需要等一段时间,

一定要确保你的registrationID获取到才能成功注册指定用户推送。


相关文档链接

http://docs.jiguang.cn/jpush/resources/#ios-sdk














  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值