iOS-极光推送集成与开发

在进行以下操作时,开发证书或者发布证书要申请成功

Mac钥匙串--》钥匙串访问--》证书助理--》从证书颁发机构请求证书--》生成CSR文件
(证书密码为极光推送上传证书时的密码)```
######登录[开发者](https://developer.apple.com/account)网站

创建AppID--》(#Bundle ID和应用一致,测试推送功能记得勾选#Push Notifications)
--》创建证书,此时需要CSR文件
下载证书
--》双击证书,右键查看简介添加信任,在钥匙串中导出为.p12文件
在极光注册应用时上传开发证书或者发布证书
--》完成注册

描述文件有问题的可以重新生成描述文件(主要作用是给应用和AppID、证书建立关联)

####1.导入静态库

下载的极光推送SDK包解压后,将其中的Lib文件拖入工程中,然后导入相关的静态库

下载SDK,将需要的两个文件导入项目中:
![jpushsdk.png](http://upload-images.jianshu.io/upload_images/1398091-12278e5fe027de26.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
```集成压缩包内容
包名为JPush-iOS-SDK-{版本号}
lib文件夹:包含头文件 JPUSHService.h,静态库文件jpush-ios-x.x.x.a ,支持的iOS版本为 5.0 及以上版本。
//(请注意:模拟器不支持APNs)
pdf文件:集成指南
demo文件夹:示例```
![静态库.png](http://upload-images.jianshu.io/upload_images/1398091-ec2a7be673e7b73a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####2.代码配置

//在AppDelegate的相关方法中进行代码配置

import "JPUSHService.h"

import

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    //Required
    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];
    }
    //Required
    // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
    [JPUSHService setupWithOption:launchOptions
    appKey:appKey
    channel:channel//发布平台,nil
    apsForProduction:isProduction//BOOL 值
    advertisingIdentifier:advertisingId
    ];
    }

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

    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
    }

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
    }

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    // IOS 7 Support Required
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    // 应用正处理前台状态下,不会收到推送消息,因此在此处需要额外处理一下
    if (application.applicationState == UIApplicationStateActive) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息"
    message:userInfo[@"aps"][@"alert"]
    delegate:nil
    cancelButtonTitle:@"取消"
    otherButtonTitles:@"确定",nil];
    [alert show];
    }
    }

  • (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [application setApplicationIconBadgeNumber:0];
    return;
    }

####3.Xcode设置

在info.plist文件中设置允许http访问
如果你的工程需要支持小于7.0的iOS系统,请到Build Settings 关闭 bitCode 选项,否则将无法正常编译通过

![http.png](http://upload-images.jianshu.io/upload_images/1398091-cc865ecb4b955c74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

设置允许推送

![Xcode.png](http://upload-images.jianshu.io/upload_images/1398091-4fd31e5a3d963070.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####4.然后链接真机运行测试成功,Xcode会打印如下:
![打印结果.png](http://upload-images.jianshu.io/upload_images/1398091-000cfcde468a26dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####5.在极光推送官网发送测试信息,测试时将应用在真机退出到后台运行
![测试信息.png](http://upload-images.jianshu.io/upload_images/1398091-de080b866100bc90.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

资料:
     [推送的自定义categories](http://my.oschina.net/u/1418722/blog/317422)
[iOS开发中的远程推送实现(最新,支持iOS9)](https://zm10.sm-tc.cn/?src=http%3A%2F%2Fwww.mamicode.com%2Finfo-detail-1124741.html&uid=48a54dfb9031a7654198c539e9001d2f&hid=55b226f6266805a848e1c24be8b9cddd&pos=1&cid=9&time=1466556151388&from=click&restype=1&pagetype=0000004000000402&bu=structure_web_info&query=ios9)
[iOS: 极光推送](http://www.cnblogs.com/XYQ-208910/p/5463802.html)

你可能感兴趣的:(iOS-极光推送集成与开发)