iOS 极光推送集成与开发

本文详细介绍如何在iOS应用中集成极光推送服务,包括证书申请、极光SDK集成步骤、Xcode配置方法及测试流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 <AdSupport/AdSupport.h>

  • (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)
复制代码

转载于:https://juejin.im/post/5a3129536fb9a044fb07c2c0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值