我正在开发一款使用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上通过终端运行此脚本,一切正常并且通知被发送,但是如果我将其上传到服务器上,则无法正常工作。我的证书是有效的,并且与脚本位于同一个文件夹中。当我尝试在服务器上运行脚本时,我会添加这行以包含证书文件并使用变量$certificate:
$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