2014-04-14 75 views
0

我正在使用iOS推送通知的MAC應用程序。我想從我的服務器上的php腳本發送通知。我的應用程序中的代碼非常適合註冊遠程通知並接收它們。我用這個PHP腳本來發送通知和它的作品也很好:無法從服務器端連接到'ssl://gateway.sandbox.push.apple.com:2195'

<?php 

// My device token here (without spaces): 
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 

// My private key's passphrase here: 
$passphrase = 'myPrivateKeyPassword'; 

// 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電腦終端,以及來自其他Mac系統運行此腳本,一切工作良好,並通知發送,但如果我上傳到我的服務器上,它不起作用。我的證書是有效的,它位於腳本的同一個文件夾中。當我嘗試在服務器上運行腳本時。然後它給出以下錯誤:

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home/iphmuvxr/public_html/Schedueling/push_notification.php on line 25

回答

0

聽起來像您沒有打開您的服務器上的端口。 請打開該端口並再次嘗試。

$telnet gateway.sandbox.push.apple.com 2195 

您可以通過以下命令來驗證