【IOS】极光推送封装

作者: 雨影 | 来源:发表于2017-07-17 15:45 被阅读142次

    集成步骤不说了,自己看文档吧:极光推送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的实现让结构更加清晰

    相关文章

      网友评论

        本文标题:【IOS】极光推送封装

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