美文网首页
集成极光推送

集成极光推送

作者: Stone_熊小叔 | 来源:发表于2017-06-26 17:07 被阅读0次

    part 1 Appkey

    极光注册应用


    part 2 证书及环境

    极光证书配置

    最后生成需要的文件

    part 3 代码部分

    在项目didFinishLaunchingWithOptions调用[self configurationJiguangWithOption:launchOptions];即可

    #pragma mark 极光推送<可直接拷贝到项目>
        
    static NSString *JiguangAppKey = @"你在极光注册的key";
    static NSString *channel = @"Publish channel";
        
    -(void)configurationJiguangWithOption:(NSDictionary *)launchingOption{
        //极光
        //Required
        //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
        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];
        
        // Optional
        // 获取IDFA
        // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
        NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        
        // Required
        // init Push
        // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
        // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
        [JPUSHService setupWithOption:launchingOption appKey:JiguangAppKey
                              channel:channel
                     apsForProduction:NO
                advertisingIdentifier:advertisingId];
    }
        
    //注册APNs成功并上报DeviceToken
    - (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        NSLog(@"My token is: %@", deviceToken);
        /// Required - 注册 DeviceToken
        [JPUSHService registerDeviceToken:deviceToken];
    }
    
    //实现注册APNs失败接口
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        //Optional
        NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }
        
    //添加处理APNs通知回调方法
        // 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三种类型可以选择设置
    }
        
        // iOS 10 Support
    - (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]]) {
            [JPUSHService handleRemoteNotification:userInfo];
        }
        completionHandler();  // 系统要求执行这个方法
    }
        
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        
        // Required, iOS 7 Support
        [JPUSHService handleRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
    

    part 4 测试结果

    真机运行结果

    part 5 推送一个试试效果?

    效果图

    只是简单版本,后面有时间再增加自定义的

    相关文章

      网友评论

          本文标题:集成极光推送

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