【IOS】极光推送封装

集成步骤不说了,自己看文档吧:极光推送iOS文档

新建一个AppDelegate的category

.h实现:

#import "AppDelegate.h"
#import "JPUSHService.h"

#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h> // iOS10注册APNs所需头文件
#endif

@interface AppDelegate (JPush) <JPUSHRegisterDelegate>

- (void)regisJPushWithOptions:(NSDictionary *)launchOptions;

@end

.m实现


#import "AppDelegate+JPush.h"

@implementation AppDelegate (JPush)

- (void)regisJPushWithOptions:(NSDictionary *)launchOptions{
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
    /*
     apsForProduction标识当前应用所使用的APNs证书环境。
     0 (默认值)表示采用的是开发证书,
     1 表示采用生产证书发布应用。
     注:此字段的值要与Build Settings的Code Signing配置的证书环境一致。
     */
    
    [JPUSHService setupWithOption:launchOptions appKey:JPush_AppKey
                          channel:@"appStore"
                 apsForProduction:DEBUG?0:1
            advertisingIdentifier:@""];
    
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定义categories
        // NSSet<UNNotificationCategory *> *categories for iOS10 or later
        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
    }
    
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
}

#pragma mark - 实现注册APNs失败接口
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

#pragma mark - 实现注册APNs
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}


#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}

#pragma mark - 添加处理APNs通知回调方法
// iOS 10 之后处理方法
- (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]]) {
       
        [self handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系统要求执行这个方法
}
//ios7-ios10 处理方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    [self handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}


//处理推送信息
- (void)handleRemoteNotification:(NSDictionary *)remoteInfo
{
    [JPUSHService handleRemoteNotification:remoteInfo];
    [self setBadge:0];
    
}

//设置角标
- (void)setBadge:(int)badge
{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
    [JPUSHService setBadge:badge];
}

@end

用法:
1.AppDelegate中导入头文件:#import "AppDelegate+JPush.h"
只需要在AppDelegate的application:didFinishLaunchingWithOptions:中调用

    [self regisJPushWithOptions:launchOptions];

即可.

自己的操作方法在中实现

- (void)handleRemoteNotification:(NSDictionary *)remoteInfo
{
    [JPUSHService handleRemoteNotification:remoteInfo];
    [self setBadge:0];
    
}

这样做的好处是抽出了push的实现让结构更加清晰

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 通常来说项目中的推送功能一般是使用第三方来实现,可以极大的降低开发时间及成本,下面就简单介绍下关于极光推送的一个简...
    zero_zql阅读 1,949评论 8 16
  • iOS SDK 集成指南SDK说明适用版本本文匹配的 SDK版本:r2.1.5 以后。查看最近更新了解最新的SDK...
    sillen阅读 893评论 0 0
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 6,798评论 10 16
  • 版本记录 前言 前一篇已经对ios新特性进行了介绍,这一篇则对ios的SDK进行说明。1. 极光推送集成(一)2....
    刀客传奇阅读 1,347评论 0 1
  • 林则徐曾经说过一句话“海纳百川,有容乃大;壁立于仞,无欲则刚。”,宽容是一种修养更是一种美德,可是宽容绝对不是胆...
    倾座城淡场梦阅读 451评论 1 3