美文网首页
Xcode8之极光推送

Xcode8之极光推送

作者: skma | 来源:发表于2016-10-09 13:43 被阅读742次

    好久没做推送了,最近越到了一些坑,但顺利搞定,简单说下步骤

    一. 首先简单说下证书(注意所以推送相关证书尽量用项目名来命名,bundleid与工程保持一致,命名注意区分测试和发布证书)

    1. 首先要有一个99$的苹果开发者账号,新建开发者证书(此处就省略了)

    2. 创建appID,要勾选Push Notifications,bundleid要与工程保持一致(Xcode8在工程中选择对应的team后会自动在该账号上创建一个appid,如图),


    3. 添加调试设备

    iTunes上可获取设备UUID

    4. 分别新建测试和发布的推送证书,下载后双击导入

    5. 新建描述文件,也是测试和发布一对

    6. 打开钥匙串会发现有一对你刚才导入的推送证书,右键到处为p12文件,注意区分测试和发布证书,注意自己设置的密码

    7. 打开极光开发者服务(有个人账号),创建应用,应用名就是自己开发项目的名字,上传应用图标,在证书的地方上传刚刚导出的两个p12文件,bundleid与工程一致,然后点击创建应用,创建成功后如图

    8. 记录,appkey是写在工程里,Master 是要给服务器的

    二、配置工程

    1. 首先下载极光的SDK并把需要的导入到工程

    2.导入依赖库

    3. Xcode8 要在这里打开开关,都为对号则表示正常,证书选自动

    4.在appdelegate中倒入头文件

    注意打开允许http请求的开关

    然后上代码

    static NSString *appKey = @"这里填极光上面的appkey";

    static NSString *channel = @"Publish channel";

    static BOOL isProduction = FALSE;

    接受协议<JPUSHRegisterDelegate>

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    /*

    ......

    */



    #pragma mark -- 远程推送

    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

    //Required

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {

    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];

    entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;

    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

    }

    else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

    //可以添加自定义categories

    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |

    UIUserNotificationTypeSound |

    UIUserNotificationTypeAlert)

    categories:nil];

    }

    else {

    //categories 必须为nil

    [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

    UIRemoteNotificationTypeSound |

    UIRemoteNotificationTypeAlert)

    categories:nil];

    }

    //如不需要使用IDFA,advertisingIdentifier 可为nil

    [JPUSHService setupWithOption:launchOptions appKey:appKey

    channel:channel

    apsForProduction:isProduction

    advertisingIdentifier:nil];

    //2.1.9版本新增获取registration id block接口。

    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {

    if(resCode == 0){

    NSLog(@"registrationID获取成功:%@",registrationID);

    }

    else{

    NSLog(@"registrationID获取失败,code:%d",resCode);

    }

    }];


    // Override point for customization after application launch.

    return YES;

    }

    #pragma mark - 获取 device Token

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    // 告诉服务器 服务器保存

    [JPUSHService registerDeviceToken:deviceToken];

    NSLog(@"%@", deviceToken);

    }

    #pragma mark - 获取信息

    /**

    *  当收到远程推送通知时调用

    *

    *  @param application 应用程序

    *  @param userInfo    具体内容

    */

    //注册失败

    - (void)application:(UIApplication *)application

    didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

    }

    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1

    - (void)application:(UIApplication *)application

    didRegisterUserNotificationSettings:

    (UIUserNotificationSettings *)notificationSettings {

    }

    // Called when your app has been activated by the user selecting an action from

    // a local notification.

    // A nil action identifier indicates the default action.

    // You should call the completion handler as soon as you've finished handling

    // the action.

    - (void)application:(UIApplication *)application

    handleActionWithIdentifier:(NSString *)identifier

    forLocalNotification:(UILocalNotification *)notification

    completionHandler:(void (^)())completionHandler {

    }

    // Called when your app has been activated by the user selecting an action from

    // a remote notification.

    // A nil action identifier indicates the default action.

    // You should call the completion handler as soon as you've finished handling

    // the action.

    - (void)application:(UIApplication *)application

    handleActionWithIdentifier:(NSString *)identifier

    forRemoteNotification:(NSDictionary *)userInfo

    completionHandler:(void (^)())completionHandler {

    }

    #endif

    // 收到通知

    - (void)application:(UIApplication *)application

    didReceiveRemoteNotification:(NSDictionary *)userInfo

    fetchCompletionHandler:

    (void (^)(UIBackgroundFetchResult))completionHandler {

    [JPUSHService handleRemoteNotification:userInfo];

    //    NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]);

    if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 || application.applicationState>0) {

    //        [rootViewController addNotificationCount];

    }

    completionHandler(UIBackgroundFetchResultNewData);

    }

    - (void)application:(UIApplication *)application

    didReceiveLocalNotification:(UILocalNotification *)notification {

    [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];

    }

    #ifdef NSFoundationVersionNumber_iOS_9_x_Max

    #pragma mark- JPUSHRegisterDelegate

    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {

    NSDictionary * userInfo = notification.request.content.userInfo;

    UNNotificationRequest *request = notification.request; // 收到推送的请求

    UNNotificationContent *content = request.content; // 收到推送的消息内容

    NSNumber *badge = content.badge;  // 推送消息的角标

    NSString *body = content.body;    // 推送消息体

    UNNotificationSound *sound = content.sound;  // 推送消息的声音

    NSString *subtitle = content.subtitle;  // 推送消息的副标题

    NSString *title = content.title;  // 推送消息的标题

    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

    [JPUSHService handleRemoteNotification:userInfo];

    NSLog(@"iOS10 前台收到远程通知:%@", [self logDic:userInfo]);

    //        [rootViewController addNotificationCount];

    }

    else {

    // 判断为本地通知

    NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);

    }

    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置

    }

    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

    NSDictionary * userInfo = response.notification.request.content.userInfo;

    UNNotificationRequest *request = response.notification.request; // 收到推送的请求

    UNNotificationContent *content = request.content; // 收到推送的消息内容

    NSNumber *badge = content.badge;  // 推送消息的角标

    NSString *body = content.body;    // 推送消息体

    UNNotificationSound *sound = content.sound;  // 推送消息的声音

    NSString *subtitle = content.subtitle;  // 推送消息的副标题

    NSString *title = content.title;  // 推送消息的标题

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

    [JPUSHService handleRemoteNotification:userInfo];

    //        NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]);

    //        [rootViewController addNotificationCount];

    }

    else {

    // 判断为本地通知

    NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);

    }

    completionHandler();  // 系统要求执行这个方法

    }

    #endif

    // if not ,log will be \Uxxx

    - (NSString *)logDic:(NSDictionary *)dic {

    if (![dic count]) {

    return nil;

    }

    NSString *tempStr1 =

    [[dic description] stringByReplacingOccurrencesOfString:@"\\u"

    withString:@"\\U"];

    NSString *tempStr2 =

    [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

    NSString *tempStr3 =

    [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];

    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];

    NSString *str =

    [NSPropertyListSerialization propertyListFromData:tempData

    mutabilityOption:NSPropertyListImmutable

    format:NULL

    errorDescription:NULL];

    return str;

    }

    这些代码极光的开发文档里都有

    然后连接设备真机调试安装,在设备提示上选择允许发送通知

    5. 安装完退出应用,打开极光的应用管理页面,选择要推送消息的应用,选择推送

    点击立即发送,打开记录,设备会有提示音,弹窗,推送成功

    6. 本人只说出自己的成功经验,如有问题请提出指导意见。

    相关文章

      网友评论

          本文标题:Xcode8之极光推送

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