从 PHP 发送 iOS 推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21237445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send iOS push notification from PHP
提问by lubilis
I'm working on an iOS app that use iOS Push Notifications. I want to send the notification from a php script on my server. The code in my app works really good for registering for remote notification and receive them. I use this php script to send notification and it works well too:
我正在开发一个使用 iOS 推送通知的 iOS 应用程序。我想从我服务器上的 php 脚本发送通知。我的应用程序中的代码非常适合注册远程通知并接收它们。我使用这个 php 脚本发送通知,它也运行良好:
<?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);
My problem is that if i run this script by terminal on my imac, all works good and notifications are sent, but if i upload it on my server it doesn't work. My certificate is valid and it's in the same folder of the script. When i try to run the script on the server, i add this line to include the certificate file and use the variable $cerificate:
我的问题是,如果我在 imac 上通过终端运行此脚本,一切正常并发送通知,但如果我将其上传到我的服务器上,则它不起作用。我的证书是有效的,它在脚本的同一个文件夹中。当我尝试在服务器上运行脚本时,我添加了这一行以包含证书文件并使用变量 $cerificate:
$certificate = file_get_contents('ck.pem');
This is the error of the php server side:
这是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
Why this happens? Do i have to add something on my server to enable pans connection?
为什么会发生这种情况?我是否必须在我的服务器上添加一些东西才能启用 pans 连接?
-----UPDATE QUESTION-----
-----更新问题-----
My first problem was that i didn't have the privileges to send connection on port 2195 and receive response on port 2196. Now, my server admin enabled these connection and i have different errors:
我的第一个问题是我没有权限在端口 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
Failed to connect: 0
连接失败:0
采纳答案by Adnan Aftab
It happens when you are using shared hosting, and hosting provider don't give you privileges to use outbound connections on port 2195 and 2196. You should use VPS or use some other providers like urbanAirship and parse.com for this. Or even you can run your own local server... for that
当您使用共享主机时会发生这种情况,并且主机提供商没有授予您在端口 2195 和 2196 上使用出站连接的权限。为此,您应该使用 VPS 或使用其他一些提供商,例如 UrbanAirship 和 parse.com。或者甚至你可以运行你自己的本地服务器......为此

