[英]Apple Push Notification from Server
我遵循了: Apple推送通知服務教程 。 它在本地為我工作。 接下來,我要從服務器發送推送通知嗎?
我已經將simplepush.php
和ck.pem
上傳到我的服務器。 當我檢查http://www.myserver/simplepush.php
時,出現錯誤:
*警告:stream_socket_client()[function.stream-socket-client]:無法連接到/ home / cherry / public_html / simplepush中的ssl://gateway.sandbox.push.apple.com:2195(連接超時)。第21行的php無法連接:110連接超時*
請你幫助我好嗎?
PHP代碼:
<?php
// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxx';
// 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);
PHP是否使用SSL編譯? 它與您的本地版本是否相同?
還是某種防火牆阻止了從服務器到2195端口的連接?
您能否登錄到該服務器(某種外殼)並檢查是否可以通過telnet連接到該服務器和端口:
$ telnet gateway.sandbox.push.apple.com 2195
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.