首先生成各种证书文件,生成方法可以看这个链接:http://tr4work.blog.163.com/blog/static/137149314201132101834828/
从生成什么p12文件开始,用下面这个链接第一个回答提到的方法:http://stackoverflow.com/questions/1481443/apple-push-notification-service
Once you have the certificate from Apple for your application, export your key and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:
- Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
- Right click and choose
Export 2 items…
.- Choose the p12 format from the drop down and name it
cert.p12
.Now covert the p12 file to a pem file:
$ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts
在生成p12文件的时候记得要同时选中推送证书跟它的密钥
注意这里的证书指的是Apple production/development push service: XXXXXXXXXX
ps:
在mac中可以自行安装php环境然后进行推送测试,环境:xampp 1.7.3 macos版本
php代码如下:
<?php // 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)?? $deviceToken = 'ee44c1bc795fc75b6f8c7db8f5133b4106217e75b2524b05843c07c9e91ed2bd'; // Put your private key's passphrase here: $passphrase = ''; // Put your alert message here: $message = 'The Fucking Apple Cert'; //////////////////////////////////////////////////////////////////////////////// $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns_dev.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server //这个为正是的发布地址 //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx); //这个是沙盒测试地址,发布到appstore后记得修改哦 $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array( 'alert' => $message, 'sounds' => 'default' ); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?>
有问题欢迎联系本人:917141931