2014-01-20 196 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