几天前,APNS 服务运行良好。唯一改变的是我将服务器从 Ubuntu 12.10 升级到 13.04。升级后我的 apns 停止工作。我不知道为什么,但这是我在服务器上唯一更改的地方。
我还尝试使用从我的 mac 生成的证书远程登录 gateway.push.apple.com:2195 一切正常。
但是当我尝试将我的服务器与 php 代码一起使用时,我得到了这个响应:
连接失败:0
<?php
// Put your device token here (without spaces):
$deviceToken = 'bd218fb8ce00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxxxxxxxxxxxx';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns.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, 360, 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);
最佳答案
尝试将正确的证书路径放在 php 脚本中,即
stream_context_set_option($ctx, 'ssl', 'local_cert',APPPATH.'../assets/apns.pem');
关于php - APNS 不再适用于 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966029/