JPushHelper.m 3.34 KB
//
//  JPushHelper.m
//  Car
//
//  Created by Javen on 2016/12/29.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "JPushHelper.h"
#import "JPUSHService.h"

static NSString *const JPUSHAPPKEY = @"5dc63eefddc7955ac9985390"; // 极光appKey
static NSString *const channel = @"蒲公英";                    // 固定的

#ifdef DEBUG                            // 开发
static BOOL const isProduction = FALSE; // 极光FALSE为开发环境
#else // 生产
static BOOL const isProduction = TRUE; // 极光TRUE为生产环境
#endif

@implementation JPushHelper

+ (void)JPUSHConfigWithOptions:(NSDictionary *)launchOptions {
  // 注册apns通知
  if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10
  {
#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];
  }
  /*
   *  launchingOption 启动参数.
   *  appKey 一个JPush 应用必须的,唯一的标识.
   *  channel 发布渠道. 可选.
   *  isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为
   * YES.
   *  advertisingIdentifier 广告标识符(IDFA) 如果不需要使用IDFA,传nil.
   * 此接口必须在 App 启动时调用, 否则 JPush SDK 将无法正常工作.
   */

  // 如不需要使用IDFA,advertisingIdentifier 可为nil
  // 注册极光推送
  [JPUSHService setupWithOption:launchOptions
                         appKey:JPUSHAPPKEY
                        channel:channel
               apsForProduction:isProduction
          advertisingIdentifier:nil];

  // 2.1.9版本新增获取registration id block接口。
  [JPUSHService
      registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if (resCode == 0) {
          // iOS10获取registrationID放到这里了, 可以存到缓存里,
          // 用来标识用户单独发送推送
          NSLog(@"registrationID获取成功:%@", registrationID);
          [[NSUserDefaults standardUserDefaults] setObject:registrationID
                                                    forKey:@"registrationID"];
          [[NSUserDefaults standardUserDefaults] synchronize];
        } else {
          NSLog(@"registrationID获取失败,code:%d", resCode);
        }
      }];
}

+ (void)registerDeviceToken:(NSData *)deviceToken {
  [JPUSHService registerDeviceToken:deviceToken];
}


@end