iOS极光推送部分

作者: 风外杏林香 | 来源:发表于2016-12-08 18:52 被阅读70次

    研究了下极光推送,最恶心的部分莫过于一系列证书的配置,
    1、注意下p12的导出,特别需要注意,
    正确方式是这样的

    正确导出方式,直接导出
    错误方式是这样的
    错误方式,打开后导出
    2、xcode 的配置

    3、导入极光推送sdk,同时,添加依赖库
      CFNetwork.framework
      CoreFoundation.framework
      CoreTelephony.framework
      SystemConfiguration.framework
      CoreGraphics.framework
      Foundation.framework
      UIKit.framework
      Security.framework
      libz.tbd (Xcode7以下版本是libz.dylib)
      AdSupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)
      UserNotifications.framework (Xcode8及以上)
      libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)
    

    真机编译,没报错,好的接下来开始敲代码
    4、在AppDelegate.m里面添加 JPUSHRegisterDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
            //iOS10以上
            JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
            entity.types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
            [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    
        } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            //iOS8以上可以添加自定义categories
            [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) categories:nil];
        } else {
            //iOS8以下categories 必须为nil
            [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)categories:nil];
            
        }
        
        BOOL isProduction = NO;// NO为开发环境,YES为生产环境
        //广告标识符
        NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        //Required(2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
        [JPUSHService setupWithOption:launchOptions appKey:APP_KEY
                              channel:nil
                     apsForProduction:isProduction
                advertisingIdentifier:advertisingId];
        //此处设置当点击icon时候进入app 销毁icon badge
        NSInteger bagValue = [UIApplication sharedApplication].applicationIconBadgeNumber;
        NSLog(@"bagValue -- %ld", bagValue);
        if (bagValue != 0) {
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
            [JPUSHService setBadge:0];
            [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
            [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
            
        }    
        return YES;
    }
    
    #pragma mark -- 注册 DeviceToken
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        [JPUSHService registerDeviceToken:deviceToken];
    }
    
    #pragma mark -- JPUSHRegisterDelegate
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler
    {
        // Required
        NSDictionary * userInfo = notification.request.content.userInfo;
        NSLog(@"userInfo -- %@", userInfo);
        if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]
            ]) {
            [JPUSHService handleRemoteNotification:userInfo];
        }
        completionHandler(UNNotificationPresentationOptionAlert); // 需要执这个方法,选择是否提醒户,有Badge、Sound、Alert三种类型可以选择设置
    }
    
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
    {
        // Required
        NSDictionary * userInfo = response.notification.request.content.userInfo;
        NSLog(@"00000userInfo -- %@", userInfo);
        if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            [JPUSHService handleRemoteNotification:userInfo];
        }
        completionHandler(); // 系统要求执行这个方法
    }
    
    #pragma mark -- iOS 7 远程请求
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
        [JPUSHService handleRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
    #pragma mark -- iOS 6 或者以下远程请求
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    

    接下来,我们从极光后台,推送一条消息过来,打印出来的userInfo

    userInfo -- {
        "_j_msgid" = 2929855611;
        aps =     {
            alert = "775wode ";
            badge = 1;
            sound = default;
        };
    }
    

    我们看到 badge = 1,同时受到的消息为 alert = "775wode"
    好的,我们再发送一条消息

    有比较就有发现
    badge 值一直为1,同时,我们发送了俩条消息,但是icon角标上面一直显示为1
    这样解决,我们直接在极光的后台设置badge值为 +1这样每次发送消息的时候,在之前的基础上面加1,数量变正常
    直接后台设置咯
    以上,就是集成极光推送发送远程通知的内容,个人认为,集成不难,难的是,集成之前各种证书的配置,生产环境,开发环境,各种烦躁,希望大家一起加油!!!

    相关文章

      网友评论

        本文标题:iOS极光推送部分

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