【发布时间】:2014-01-20 15:11:39
【问题描述】:
我正在开发一个使用 iOS 推送通知的 iOS 应用程序。我想从我的服务器上的 php 脚本发送通知。我的应用程序中的代码非常适合注册远程通知并接收它们。我使用这个 php 脚本来发送通知,它也很好用:
<?php
// My device token here (without spaces):
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// My private key's passphrase here:
$passphrase = 'myPrivateKey';
// My alert message here:
$message = 'New Push Notification!';
//badge
$badge = 1;
$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,
'badge' => $badge,
'sound' => 'newMessage.wav'
);
// 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 'Error, notification not sent' . PHP_EOL;
else
echo 'notification sent!' . PHP_EOL;
// Close the connection to the server
fclose($fp);
我的问题是,如果我在 imac 上通过终端运行此脚本,一切正常并发送通知,但如果我将它上传到我的服务器上,它就不起作用。我的证书是有效的,它在脚本的同一个文件夹中。当我尝试在服务器上运行脚本时,我添加了这一行以包含证书文件并使用变量 $cerificate:
$certificate = file_get_contents('ck.pem');
这是php服务器端的错误:
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in script.php on line 29
Failed to connect: 110 Connection timed out
为什么会这样?我是否必须在我的服务器上添加一些东西才能启用 pans 连接?
-----更新问题-----
我的第一个问题是我没有权限在端口 2195 上发送连接并在端口 2196 上接收响应。现在,我的服务器管理员启用了这些连接,但我遇到了不同的错误:
Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in script.php on line 28
Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in script.php on line 28
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in script.php on line 28
连接失败:0
【问题讨论】:
-
你能建立一个从你的服务器到 APNS 服务器的 telnet 连接(从服务器控制台)吗?
-
有时服务器不允许您使用所需的端口...最好询问您的服务器管理员并确认您是否有权限...您是否使用共享主机...?这发生在共享主机中...
-
是的,我正在使用共享主机,我刚刚写信给我的服务器管理员。 @rokjarc 抱歉,但我无法测试 telnet 连接,因为我不知道该怎么做,抱歉,我是新程序员 :(
-
如果您可以访问服务器控制台(不确定是否可以),您可以输入
telnet gateway.push.apple.com 2195并观察响应 -
不,我认为无法访问它。我可以访问数据库、控制面板或FTP
标签: php ios notifications