2014-04-14 74 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 

您可以通过以下命令来验证