{"id":247,"title":"php \u63a8\u9001\u6d88\u606f\u5230ipad","good":0,"bad":0,"hit":1418,"created_at":"2016-11-10 17:20:43","content":"
\u5f53app\u6700\u5c0f\u5316\u540e\uff0c\u53ef\u4ee5\u5728\u901a\u77e5\u680f\u5f39\u51fa\uff0cios8\u8c8c\u4f3c\u4e0d\u652f\u6301\uff0c\u8fd9\u662fiso9\u4e0b\u6d4b\u8bd5\u8fc7\u7684
<\/p>
<\/p>
private function send_msg($deviceToken, $message) {
\/\/ ??????????deviceToken???????????????
\/\/$deviceToken = '\u4f60\u81ea\u5df1\u4ece\u82f9\u679c\u4e0a\u83b7\u5f97\u7684token';
\/\/ Put your private key's passphrase here:
$passphrase = '\u79c1\u94a5;
\/\/ Put your alert message here:
\/\/ $message = 'hr test';
$pem=str_replace('\\\\',DIRECTORY_SEPARATOR,dirname(__FILE__).'\\\u60a8\u7684key.pem');
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
\/\/?????????????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,
'sound' => '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);
}<\/p>"}