首页 > 代码库 > iOS 推送证书

iOS 推送证书

push 服务器证书

钥匙串:登入--》证书,选项里面导出证书命名为cert.p12,跟密钥命名为key.p12

需要将上面的2个.p12文件转成.pem格式:

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

 

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

 

如果需要对 key不进行加密:

  

openssl rsa -in key.pem -out key.unencrypted.pem

 

然后就可以 合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。

  

cat cert.pem key.unencrypted.pem > ck.pem

 

4个pem,另外加上php文件,打包放到服务器上

 

php推送代码

 1 <?php
 2     $deviceToken = ‘c9b4b78b a78d9b3a 9104690f 95c35aa9 2e0e3c6a 6d3edb68 60b4571a fdcb6b3f‘;    //loganv-itouch
 3     // Get the parameters from http get or from command line
 4     $message = $_GET[‘message‘] or $message = $argv[1] or $message = ‘hi from loganv!‘;
 5     $badge = (int)$_GET[‘badge‘] or $badge = (int)$argv[2] or $badge = 9;
 6     $sound = $_GET[‘sound‘] or $sound = $argv[3] or $sound = ‘default‘;
 7     // Construct the notification payload
 8     $body = array();
 9     $body[‘aps‘] = array(‘alert‘ => $message);
10     if ($badge)
11     $body[‘aps‘][‘badge‘] = $badge;
12     if ($sound)
13     $body[‘aps‘][‘sound‘] = $sound;
14     /* End of Configurable Items */
15     $ctx = stream_context_create();
16     stream_context_set_option($ctx, ‘ssl‘, ‘local_cert‘, ‘ck.pem‘);
17     // assume the private key passphase was removed.
18     //stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $pass);
19     // connect to apns
20     $fp = stream_socket_client(‘ssl://gateway.sandbox.push.apple.com:2195‘, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
21     if (!$fp) {
22         print "Failed to connect $err $errstr\n";
23         return;
24     }
25     else {
26         print "Connection OK\n<br/>";
27     }
28     // send message
29     $payload = json_encode($body);
30     $msg = chr(0) . pack("n",32) . pack(‘H*‘, str_replace(‘ ‘, ‘‘, $deviceToken)) . pack("n",strlen($payload)) . $payload;
31     print "Sending message :" . $payload . "\n";  
32     fwrite($fp, $msg);
33     fclose($fp);
34     ?>