apns推送

发个广告:ios开发两年了,一步步走来  关注公众号一起进步

看到的文章记录


1,环境配置

APNS:Apple Push Notification Service。本文对推送相关概念不再赘述,只侧重完整流程。 

Demo 开发环境:Mac os 10.9.4  ,Xcode 6.0.1 ;测试设备:iphone 4s(ios 7.1)

服务端开发环境:mac 10.9.4  + php 5.4.24、

Demo 下载地址:点击打开链接

2,APNS 相关博客

如对apns相关概念不清楚,可参考以下几个博客:(博客中部分内容重复,但总体来说,通读一遍,还是大有裨益的)

 http://cshbbrain.iteye.com/blog/1859810  =》IOS 基于APNS消息推送原理与实现(JAVA后台)

http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html   =》iOS消息推送机制的实现

http://blog.youkuaiyun.com/xunyn/article/details/8243573  =》APNS编程----iOS真机测试消息推送

http://blog.youkuaiyun.com/wswqiang/article/details/8208581  =》IOS APNS 处理

http://eric-gao.iteye.com/blog/1567777  =》 IOS PEM 文件的生成

http://www.36coder.com/study/996.html  =》PHP 实现APNS 推送

http://blog.youkuaiyun.com/sxfcct/article/details/7939082  =》 APNS 相关总结(推荐)

3,APNS 接口

消息推送:

开发接口:gateway.sandbox.push.apple.com:2195

发布接口:gateway.push.apple.com:2195

反馈服务:

开发接口:feedback.sandbox.push.apple.com:2196

发布接口:产品接口:feedback.push.apple.com:2196

二、制作Push证书和Pem文件

1,新建一个App ID

新建流程不再赘述,这里只提醒两点:1》App ID Suffix 中,一定要选择Explicit App ID;2》App Services 中,记得勾选Push Notifications。这里以新建一个id为:com.eversoft.PushDemo 为例。

2,配置push开发证书

在App IDs中,选中刚才新建的App id:com.eversoft.PushDemo ,单击,展开详细信息属性。

在详细信息属性中,单击下方的“Edit”按钮,
在新打开的编辑界面,单击“Create Certificate”,

在新打开的界面中,会提示我们,创建一个csr 证书签名请求文件。具体的创建步骤,界面中已经给出了详细的英文说明。

在进行下一步之前,我们先按照英文说明,创建一个 CSR 文件。
  • 在mac电脑上,打开应用程序  keychain(钥匙串访问);
  • 在keychain菜单栏中,依次选择“钥匙串访问”=》“证书助理”=》“从证书颁发机构请求证书”;
  • 在新打开的“证书助理”界面中,填写用户电子邮件地址,常用名称,CA电子邮件地址,这两个邮件地址直接填写你的苹果账号的邮件地址即可,然后选择“存储到磁盘”,然后点击“继续”;
  • 选择CSR文件保存位置,“存储”即可。至此, CSR 文件,制作完成。

回到刚才我们的web页面上,点击“Continue”,进入下一页面;新的页面中,会要求我们上传刚才制作的csr文件,选择“Choose File”,找到我们刚才存储的csr文件,单击“打开”,最后,点击页面上的“Generate”按钮,到此,开发使用的push证书制作完毕。
证书生成成功后,选择“Download”,将制作好的证书下载到本地。然后双击下载的证书aps_development.cer,双击后,证书就自动导入到钥匙串中了。

打开 keychain,左侧钥匙串选择“登录”,种类选择“所有项目”,在右侧窗口中,选中刚才导入的Apple Development IOS Push Services证书(不用选中专用密钥),右键,选择导出,命名为:ck.p12 ,存储时,会提示输入保护密码,这里为演示方便,就输入了123456。之后又会要求输入电脑登录密码,输入即可。

3,生成PEM文件

最后,打开终端,执行以下命令,生成pem文件

openssl pkcs12 -in ck.p12 -out ck.pem -nodes 

执行时,会要求输入导入密码,这里输入刚才的保护密码123456即可。


到此,php 服务端使用的pem证书就制作完毕了。

Development PP 文件制作不再赘述。

三、IOS 代码编写

首先,在AppDelegate.m 中:

1,注册通知

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     // Override point for customization after application launch.  
  3.     ViewController *mainCtrl=[[ViewController alloc] init];  
  4.     self.window.rootViewController=mainCtrl;  
  5.       
  6.     //注册通知  
  7.     if ([UIDevice currentDevice].systemVersion.doubleValue<8.0) {  
  8.         [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];  
  9.     }  
  10.     else {  
  11.         [[UIApplication sharedApplication] registerForRemoteNotifications];  
  12.         [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];  
  13.     }  
  14.       
  15.     //判断是否由远程消息通知触发应用程序启动  
  16.     if (launchOptions) {  
  17.         //获取应用程序消息通知标记数(即小红圈中的数字)  
  18.         NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;  
  19.         if (badge>0) {  
  20.             //如果应用程序消息通知标记数(即小红圈中的数字)大于0,清除标记。  
  21.             badge--;  
  22.             //清除标记。清除小红圈中数字,小红圈中数字为0,小红圈才会消除。  
  23.             [UIApplication sharedApplication].applicationIconBadgeNumber = badge;  
  24.             NSDictionary *pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];  
  25.               
  26.             //获取推送详情  
  27.             NSString *pushString = [NSString stringWithFormat:@"%@",[pushInfo  objectForKey:@"aps"]];  
  28.             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"finish Loaunch" message:pushString delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil nil];  
  29.             [alert show];  
  30.         }  
  31.     }  
  32.       
  33.     return YES;  
  34. }  

2,注册通知后,获取device token

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {  
  2.     NSString *token = [NSString stringWithFormat:@"%@", deviceToken];  
  3.     NSLog(@"My token is:%@", token);  
  4.     //这里应将device token发送到服务器端  
  5. }  
  6.   
  7. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {  
  8.     NSString *error_str = [NSString stringWithFormat@"%@", error];  
  9.     NSLog(@"Failed to get token, error:%@", error_str);  
  10. }  

3,接收推送通知

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
  2. {  
  3.     [UIApplication sharedApplication].applicationIconBadgeNumber=0;  
  4.     for (id key in userInfo) {  
  5.         NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);  
  6.     }  
  7.     /* eg. 
  8.     key: aps, value: { 
  9.         alert = "\U8fd9\U662f\U4e00\U6761\U6d4b\U8bd5\U4fe1\U606f"; 
  10.         badge = 1; 
  11.         sound = default; 
  12.     } 
  13.      */  
  14.     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"remote notification" message:userInfo[@"aps"][@"alert"] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil nil];  
  15.     [alert show];  
  16. }  

注意:app 前台运行时,会调用 remote notification;app后台运行时,点击提醒框,会调用remote notification,点击app 图标,不调用remote notification,没反应;app 没有运行时,点击提醒框,finishLaunching   中,launchOptions 传参,点击app 图标,launchOptions 不传参,不调用remote notification。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值