Apple PUSH Notication Service (APNS) 配置攻略

iOs 3.0以后就支持APNS( apple push notication Service).下面介绍怎么配置APNS服务。

APNS 分为客户端与服务器端2个部分:

客户端部分:

 1    创建一个App Id 。如果你已经是apple的注册用户(至少是开发者)。首先登录进入Apple developer program portal

 2   创建好App Id 点击 Configure。选择支持APNS,按照步骤选择你的开发证书(csr)。并下载cer文件双击安装( Apple development iOS push notication Services:xxxxxxxxx)

3    此时 。Mac 上的 keychain 里面 Login 已经包含了想要的证书和密钥,按Control 将 证书和密钥export出。保存为cert.p12 和 key.p12 文件(导出过程需要输入密码。请务必保留这个密码)

4    打开终端。 cd 到保护cert.p12 key.p12的文件夹。将cert.p12 he key.p12转化为pem文件,并进行合并

 openssl pkcs12 -clcerts -nodes -out cert.pem -in cert.p12

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

cat cert.pem key.pem > yourapp.pem

5  生产对应App Id 的 privosion file。并加入XCode。用这个证书对你的应用进行签名(注意,App ID 必须匹配)

6   APNS 客户端主要涉及下面几个API

 6.1 将app注册notification里面.

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        
  2.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];  
  3.         // other codes here.    
  4.     return YES;
  5. }
6.2  从APNS上获取测试机的deviceToken.

  1. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  2.     NSLog(@"deviceToken: %@", deviceToken);
  3. }
  4.  
  5. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  6.     NSLog(@"Error in registration. Error: %@", error);
  7. }
3  当收到 PUSH 的时候,处理

  1. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  2. {
  3.     
  4.     NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
  5.     if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
  6.         UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
  7.                                                         message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
  8.                                                        delegate:self
  9.                                               cancelButtonTitle:@" 关闭"
  10.                                               otherButtonTitles:@" 更新状态",nil];
  11.         [alert show];
  12.         [alert release];
  13.     }


客户端部分基本上完成了,需要保留好 pem文件,p12文件的密码,和获得的DeviceToke,服务器方面需要使用到


服务器方面:

1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:
    

  1. <?php
  2. $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b'; // 可以用你获得的DeviceToken替换
  3. $pass = '123456';   // Passphrase for the private key (ck.pem file)  
  4.  
  5. // Get the parameters from http get or from command line
  6. $message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';
  7. $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
  8. $sound = $_GET['sound'] or $sound = $argv[3];
  9.  
  10. // Construct the notification payload
  11. $body = array();
  12. $body['aps'] = array('alert' => $message);
  13. if ($badge)
  14.   $body['aps']['badge'] = $badge;
  15. if ($sound)
  16.   $body['aps']['sound'] = $sound;
  17.  
  18. /* End of Configurable Items */
  19. $ctx = stream_context_create();
  20. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');  
  21. // assume the private key passphase was removed.
  22. stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
  23.  
  24. // connect to apns
  25. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  26. if (!$fp) {
  27.     print "Failed to connect $err $errstr\n";
  28.     return;
  29. }
  30. else {
  31.    print "Connection OK\n<br/>";
  32. }
  33.  
  34. // send message
  35. $payload = json_encode($body);
  36. $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
  37. print "Sending message :" . $payload . "\n";  
  38. fwrite($fp, $msg);
  39. fclose($fp);
  40. ?>

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

将php部署好,在IE中访问即可。


经过测试,可以在真机上获得Push消息。

在iPhone4.3已测试通过。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值