我面临的问题是 APNS 的服务器端实现。创建的示例应用程序得到了令牌创建的服务器 php 代码创建者证书和所有在本地服务器上都可以与 mamp 完美配合。[也在其他机器上测试过]
我面临的问题是当在服务器机器中设置代码时,它显示“创建 ssl 套接字时出错”连接超时。
出现错误
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home/push.php on line 16
有任何想法吗?
找到并尝试过的解决方案
- 开放2195端口
是否有必要创建一个新证书并将其全部托管在服务器中?
编辑
当前使用的推送代码
<?php
function sendApnsPushMessage($arrtokens,$req_message,$req_title)
{
echo "Started APNS";
if(count($arrtokens)>0)
{
$apnsCert = 'ck.pem';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
// Connect to Apple Push Notification server
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', '1234');
//60 -> timeout in seconds
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 200, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext);
if (!$apns)
{
echo($error);
die('Error creating ssl socket');
}
stream_set_blocking ($apns, 0);
echo 'Connected to APNS' . PHP_EOL;
for($i=0;$i<count($arrtokens);$i++)
{
$tokenid = $arrtokens[$i];
echo($tokenid);
$body['aps'] = array(
'title' => $req_title,
'alert' => $req_message,
'sound' => 'default'
);
$payload = json_encode($body);
$correctingToken=str_replace(' ', '', $tokenid);
echo $correctingToken;
$msg = chr(0) . pack('n', 32) . pack('H*', $correctingToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($apns, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
}
usleep(500000);
fclose($apns);
}
}
$token= "dd3e343519524a986f946db0f888e895961b370720ee1c9b2495d0a58542607e";
$req_title='test';
$req_message='text';
$arrayOfTokens = array($token);
sendApnsPushMessage($arrayOfTokens,$req_message,$req_title);
?>