我正在使用iOS推送通知的iOS應用程序。我想從我的服務器上的php腳本發送通知。我的應用程序中的代碼非常適合註冊遠程通知並接收它們。我用這個PHP腳本來發送通知和它的作品也很好:從PHP發送iOS推送通知
<?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:
這是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
爲什麼會發生這種情況?我必須在我的服務器上添加一些東西來啓用平底鍋連接嗎?
UPDATE ----- -----問題
我的第一個問題是,我沒有足夠的權限發送端口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連接,因爲不知道我該怎麼做,對不起,但我是一個新的程序員:( – lubilis