APNS推送通知的流程

Java: https://github.com/notnoop/java-apns
下面是我的所有部署配置过程。

1. 将app注册notification里面, 并从 APNS上获取测试机的deviceToken.       
复制代码
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        
  2.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];  
  3.         // other codes here.    
  4.     return YES;
  5. }
  6. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  7.     NSLog(@"deviceToken: %@", deviceToken);
  8. }
  9. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  10.     NSLog(@"Error in registration. Error: %@", error);
  11. }
  12. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  13. {
  14.     
  15.     NSLog(@"收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
  16.     if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
  17.         UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
  18.                                                         message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
  19.                                                        delegate:self
  20.                                               cancelButtonTitle:@"关闭"
  21.                                               otherButtonTitles:@"更新状态",nil];
  22.         [alert show];
  23.         [alert release];
  24.     }
  25. }

启动程序,将app注册到通知项后,在console里面找到打印的deviceToken:
复制代码
  1. deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>


2. 生成app在服务端需要的许可证
1)进入Provisioning Portal, 下载Certificates在development下的证书。
2) 找到需要测试的app id,然后enable它在development下的Apple Push Notification service: Development Push SSL Certificate。需要输入1)中的签名证书才可以生成一个aps_developer_identity.cer.
3) 双击aps_developer_identity.cer,会打开系统的key chain. 在My certificates下找到Apple Development Push Services。需要为certificate和它之下的private key各自export出一个.p12文件。(会出现设置密码过程)
4)需要将上面的2个.p12文件转成.pem格式:
复制代码
  1. openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12

复制代码
  1. openssl pkcs12 -nocerts -out key.pem -in key.p12

5)如果需要对key不进行加密:
   
复制代码
  1. openssl rsa -in key.pem -out key.unencrypted.pem

6)然后就可以合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
   
复制代码
  1. cat cert.pem key.unencrypted.pem > ck.pem


3.服务端push通知到ANPS. 在cocoachina找到了两种方法:
    1)php驱动。需要将ck.pem和php脚本放到server上。全部的php代码是:
       
复制代码

请求一次 http://127.0.0.1/apns/apns.php?message=A test message from localhost&badge=2&sound=received5.caf就会向 APNS进行一次推送。我的请求结果如下:
复制代码
  1. Connection OK
  2. Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}}


    2)pushMeBaby驱动。将aps_developer_identity.cer导入到project里面,改名为 apns.cer。
启动app即可看到 push窗口。pushMeBaby和具体的配置的过程也基本是参考这个帖子的,非常感谢cocoachina会员的奉献精神。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值