【发布时间】:2014-07-25 23:42:21
【问题描述】:
几天前,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);
【问题讨论】:
-
确保您的 pem 文件可以从您的路径访问
标签: php ios xcode ubuntu apple-push-notifications