极光推送的从0到1的配置

介绍一下极光推送的从头配置,包括极光的,和apple developer的配置。

1.首先注册号极光推送的账号,并创建新应用,填写应用名称以及图标。

2.创建好应用,进去完成推送设置,iOS版本需要上传后缀为p12的生产证书和开发证书,这些在apple developer里获取。

3.生成推送开发证书:主要是要注意选择Development选项。

       a.点击continue,进入App ID 界面;

       b.选好应用的App ID,然后上传本机的CSR文件;

       c.CSR文件上传成功之后,点击继续,然后下载文件即可。

4. 生成推送生产证书:创建步骤和推送开发证书的步骤一样,主要是要注意选择Production选项。

       a.点击continue,进入App ID 界面;

       b.选好应用的App ID,然后上传本机的CSR文件;

       c.CSR文件上传成功之后,点击继续,然后下载文件即可。

 

5.下载两个生成好的证书,从钥匙串中分别导出对应的p12文件。

        a.下载两个生成好的证书,然后分别双击打开,然后再钥匙串中找到这两个证书位置;

        b.分别右键选中证书导出对应的p12文件,设置密码;

        c.然后上传到极光推送应用管理里面需要上传的对应的p12文件即可。

好了,所以项目以外的配置就已经配置好了。 接下来进行项目里的配置。

6.首先,集成极光SDK,可以通过cocoapod ,也可以手动集成。具体详情,参照极光文档,地址为https://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/。

7.先打开项目的推送开关。

然后在plist文件里配置NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads。 两个属性均为Boolean类型,值分别为YES、YES。

在appdelegate中,导入头文件

#import "AppDelegate.h"
#import"JPUSHService.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
@interface AppDelegate ()<JPUSHRegisterDelegate>

//在系统方法里面申请通知权限,这里最高只适用到xcode8.1(iOS10.1.2),以后如果出现iOS11在做更新改变

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //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:launchOptions appKey:appkey
                          channel:@"App Store"
                 apsForProduction:isProduction
            advertisingIdentifier:nil];
 
    return YES;

}

//注册APNs成功并上报DeviceToken

//请在AppDelegate.m实现该回调方法并添加回调方法中的代码
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)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通知回调方法

//请在AppDelegate.m实现该回调方法并添加回调方法中的代码
#pragma mark- JPUSHRegisterDelegate

// ios 10 support 处于前台时接收到通知


// 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);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[UIApplication alloc] setApplicationIconBadgeNumber:0];
}

// 点击之后badge清零
- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    [application setApplicationIconBadgeNumber:0];
    [[UNUserNotificationCenter alloc] removeAllPendingNotificationRequests];
}

8.结束,可以在极光推送里发送通知试一下。

猜你喜欢

转载自blog.csdn.net/guosiyuan1993/article/details/79663661