我的推送通知有些问题。我已经阅读了大量教程,并提出了下面的脚本。我似乎无法连接到 APN。澄清一下,我没有为应用程序编写应用程序端代码。我只知道 PHP 和其他基于 Web 的语言。
我已经完成了从我的证书和私钥创建 .pem 文件的整个过程。我的网络服务器也有一个 SSL 证书(我是通过我的网络主机提供商完成的,不确定这是否是正确的做法)。我只是不确定究竟要发生什么才能测试一切是否正常。
当我在网页上加载脚本时出现错误:警告:stream_socket_client() [function.stream-socket-client]:无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接超时)连接失败:110 连接超时
能否请您查看代码并帮助我找出无法连接到 APN 的原因。还有一种方法可以获得更详细的错误报告吗?
这是我的代码:(问题应该在前 15 行左右。)
<?PHP
$username = $_GET['username'];
$userIDarr = $_GET['userARR'];
$message = $username.' is d2P';
$passphrase = 'mypass'; // not sure what this refers too.. (from what i have read I think it is meant to be for the iphone side of things)
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'ck.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
// Open a connection to the APNS server
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 10, STREAM_CLIENT_CONNECT, $streamContext);
if (!$apns)
exit("Failed to connect: $error $errorString" . PHP_EOL);
echo 'Connected to Apple service. ' . PHP_EOL;
@mysql_connect ("localhost","down2par_down2pa","4m329aMh") or die ("could not connect to MySQL");
@mysql_select_db ("down2par_d2pdb") or die ("no database");
// create array of all device IDs AND badges that will be receiving notifications
$SQLarr = implode(" AND userid =", $userIDarr);
$DB = mysql_query("SELECT diviceID, badge FROM new_fb_users WHERE userid = '$SQLarr'");
while($DBarr = mysql_fetch_array($DB)) {
$deviceToken = $DBarr['deviceID'];
$badge = $DBarr['badge'];
$id = $DBarr['id'];
mysql_query("UPDATE new_fb_users SET badge = badge+1 WHERE id = $id");
// create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => ($badge > 0 ? $badge + 1 : 1)
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0).pack('n', 32).pack('H*', str_replace(' ', '', $deviceToken)).pack('n', strlen($payload)).$payload;
// Send it to the server
$result = fwrite($apns, $msg, strlen($msg));
if (!$result)
echo 'Failed message'.PHP_EOL;
else
echo 'Successful message'.PHP_EOL;
}
// Close the connection to the server
fclose($apns);
?>