1. 本地证书合成
rm *.pem echo "export cert..." openssl pkcs12 -clcerts -nokeys -out push_cert.pem -in push_cert.p12 echo "export key..." openssl pkcs12 -nocerts -out push_key.pem -in push_key.p12 openssl rsa -in push_key.pem -out push_key_noenc.pem echo "combine together" cat push_cert.pem push_key_noenc.pem > apns-dev.pem echo "done"
其中push_cert.p12和push_key.p12分别是中keychain中根据开发证书导出来的证书和私钥。
上述脚本最终会导出一个名称为“apns-dev.pem”的认证文件。
下面会使用这个认证文件进行sanbox推送
2. 沙盒推送
<?php $deviceToken= ‘86441e1421a239d0f7eed26ab4c8bd9a9ccec21b‘; $body = array("aps" => array("alert" => ‘message‘, "badge" => 2, "sound"=>‘default‘)); $ctx = stream_context_create(); stream_context_set_option($ctx,"ssl","local_cert","apns-dev.pem"); $pass = "aabb11"; stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $pass); //$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); // for real push service $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); //for sandbox if (!$fp) { echo "Failed to connect $err $errstrn"; return; } print "Connection OK\n"; $payload = json_encode($body);$msg = chr(0) . pack("n",32) . pack("H*", str_replace(‘ ‘, ‘‘, $deviceToken)) . pack("n",strlen($payload)) . $payload; echo "sending message :" . $payload ."\n"; fwrite($fp, $msg); fclose($fp); ?>
其中deviceToken是苹果手机的40位的UDID。
这是实习的时候学到的,但是最终这个方案没有测试成功:
问题是:
php pushtest.php
Warning: stream_socket_client(): Failed to enable crypto in pushtest.php on line 10
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in pushtest.php on line 10
真的非常遗憾,希望以后还有机会学习IOS方面的内容!
时间: 2024-10-12 01:28:54