【发布时间】:2012-10-24 17:14:10
【问题描述】:
我的应用程序中的推送通知有问题。我刚开始使用推送通知,所以我不知道我的问题是什么原因,我在谷歌中找不到解决方案。 因此,我以本教程http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 作为推送通知的编程示例。当我在终端上运行“php simplepush.php”时,一切都很好,我在设备上收到了通知。但是当我在服务器上加载该脚本并尝试从那里运行它时,没有执行任何操作。只有 30 秒的等待和消息“连接失败:110 连接超时”
这里是我的脚本代码
<?php
Put your device token here (without spaces):
$deviceToken = '***************';
// Put your private key's passphrase here:
$passphrase = '**************';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$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);
【问题讨论】:
-
终端和服务器是否在同一台主机上?您是否已验证主机可以使用来自主机的 SSL 连接到 gateway.sandbox.push.apple.com?您可能需要在主机上安装 SSL 证书。
-
抱歉,我不清楚如何检查主机上是否安装了 SSL 证书。我怎样才能检查它?顺便说一句,我们的主机是由 bluehost 支持的,而 bluehost 提供安装其中一个 SSL 证书来赚钱?这是你说的证书吗?还有一件事 - 如果我从 'ssl://gateway.sandbox.push.apple.com:2195' 中删除“沙盒”,通知将不会从终端发送到设备上,即使是从我的 Mac 上。但是仍然有一条消息'消息已成功传递'
-
如果您已经解决了这个问题,您可以在下面发布解决方案吗?它可能会帮助处于类似情况的其他人。谢谢。
-
问题是2195端口被关闭了。当我为我的域打开端口时(在 bluehost 支持团队的帮助下),一切都变得很棒