xcode8简单集成极光推送

写在前面

1.推送技术产生的场景

  • 服务器端主动性: 客户端与服务器交互都是客户端主动的, 服务器一般不能主动与客户端进行数据交互, 因为服务器端无法得知客户端的 IP 地址 及 状态;
  • 数据实时性: 如果服务器端有紧急数据要传递给客户端, 就必须主动向客户端发送数据;
  • 基本原理: 使客户端实时获取服务器端消息, Pull 方式, 小周期轮询, 费电费流量; 另一个就是 Push 方式, 服务器端向客户端主动推送数据, 可以省电省流量。
    2.极光推送的原理
  • .net应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。
  • APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。
  • iPhone把发来的消息传递给相应的应用程序, 并且按照设定弹出Push通知。

项目实现

1.关于环境的配置·证书申请,这个视频里都有,我相信比我写出来的好,不懂的可以直接的看这个视频,在这里就不赘述。视频地址如下。
https://community.jiguang.cn/t/topic/6568
2.可能遇到的坑
根据上面的视频配置好之后,可能遇到的坑,下面是我自己配置之后,没法实现的原因,仅供参考。
配置好之后,下面图片的设置,如果是这样就ok了

WechatIMG48.jpeg
WechatIMG49.jpeg
WechatIMG50.jpeg
WechatIMG52.jpeg

只要上面的配置是这样的状态了,就可以添加代码实现推送的功能了

代码实现

  • 需要导入的头文件和代理
#import "JPUSHService.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h> // 这里是iOS10需要用到的框架
#endif
@interface AppDelegate ()<JPUSHRegisterDelegate>//新版的需要实现这个代理方法
  • 用到的变量
static NSString *const JPUSHAPPKEY  = @"极光appkey";//极光appkey
static NSString *const channel = @"Publish channel";//固定的
  • 注册及实现推送的全部代码
    1.注册极光推送
 //注册apns通知
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)//ios大于10.0
    {
#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) // iOS8, iOS9
    {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
    }
    else // iOS7
    {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
    }
    
    //注册极光推送
    
    [JPUSHService setupWithOption:launchOptions appKey:JPUSHAPPKEY
                          channel:channel
                 apsForProduction:false
            advertisingIdentifier:nil];
    
    [application setApplicationIconBadgeNumber:0];

2.推送的实现

/**
 JPush
 */
#pragma mark - 注册推送回调获取 DeviceToken
#pragma mark -- 成功并上报DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // 注册成功
    // 极光: Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}
#pragma mark -- 实现注册APNs失败接口
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    // 注册失败
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#pragma mark - iOS10: 收到推送消息调用(iOS10是通过Delegate实现的回调)
#pragma mark- JPUSHRegisterDelegate
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
// 当程序在前台时, 收到推送弹出的通知
// 当程序在前台时, 收到推送弹出的通知
- (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];
        NSString *message = [NSString stringWithFormat:@"will%@", [userInfo[@"aps"] objectForKey:@"alert"]];
        NSLog(@"iOS10程序在前台时收到的推送: %@", message);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil, nil];
        [alert show];
    }
    
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
    
    
    
}


#pragma mark- JPUSHRegisterDelegate
// 程序关闭后, 通过点击推送弹出的通知
- (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];
        
       // 这个是程序打开之后会有一个弹框的提示
                NSString *message = [NSString stringWithFormat:@"did%@", [userInfo[@"aps"] objectForKey:@"alert"]];
                NSLog(@"iOS10程序关闭后通过点击推送进入程序弹出的通知: %@", message);
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil,nil];
                [alert show];
    }
    
    completionHandler();  // 系统要求执行这个方法
}
#endif

结语

  • 上面的关于环境的配置之类的还请看视频和官方的文档,文档里面的都很详细
  • 我这里做的效果是推送的条数的角标是1,当打开app的时候,这个角标就会消失,如果角标想和具体的推送的条数一样,还需要后台的配合
  • 这里做的就是全部的推送,没有标签,后续会更新,针对不同用户的推送。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容