2014-01-20 69 views
6

我正在使用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

+0

你能從你的服務器到APNS服務器(從服務器控制檯)進行telnet連接嗎? –

+1

某些時間服務器不允許您使用所需的端口...更好地詢問您的服務器管理員並確認您是否擁有特權...您是否使用共享主機...?這發生在共享主機... –

+1

是的,我使用共享主機,我剛剛寫信給我的服務器管理員。 @rokjarc對不起,但我無法測試一個telnet連接,因爲不知道我該怎麼做,對不起,但我是一個新的程序員:( – lubilis

回答

5

當你正在使用共享的主機,以及託管服務提供商不給你權限,在端口2195使用出站連接它發生和2196.你應該使用VPS或使用其他公關像urbanAirship和parse.com這樣的oviders。或者甚至你可以運行你自己的本地服務器......對於那個

+0

好吧,我使用共享主機,所以這是我的問題。我的服務器管理員不能授予我使用這些端口的權限嗎?如果我在嘗試建立連接時更改端口號,會發生什麼情況? – lubilis

+0

您將需要這些端口作爲APNS服務將通過這些端口2195發送和2196反饋服務通信,但防火牆通常會阻止它們。您必須諮詢您的託管服務提供商。 –

+0

在端口上啓用我的權限後,我更新了我的問題 – lubilis