苹果推送通知..拒绝?
我需要使用 php 服务器实现苹果推送通知。我认为连接工作正常,但由于某种原因,该消息被苹果服务器拒绝。
我收到此错误:
“错误 0errorString
\n致命错误:调用未定义 函数socket_close()在 /home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php 在线 420
\n"
这是我的简单代码(带有虚假消息,仅供测试):
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => unread, 'sound' => 'default');
$payload['server'] = array('serverId' => $serverId, 'name' => $name);
$payload = json_encode($payload);
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'ck.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
echo "error " . $error;
echo "errorString" . $errorString;
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
echo "Sent";
谢谢
I need to implement apple push notifications with a php server. I think the connection is correctly working but the message is refused by apple server for some reason.
I'm getting this error:
"error 0errorString
\nFatal error: Call to undefined
function socket_close() in
/home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php
on line 420
\n"
This is my simple code (with a fake message, just for testing):
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => unread, 'sound' => 'default');
$payload['server'] = array('serverId' => $serverId, 'name' => $name);
$payload = json_encode($payload);
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'ck.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
echo "error " . $error;
echo "errorString" . $errorString;
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
echo "Sent";
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我猜您的 PHP 版本可能没有安装或启用套接字功能,这就是您收到该错误的原因。
摘自 PHP.net:
但对于您的代码,您不需要使用该
socket_close($apns)
函数。相反,您可以删除它并使用关闭,因为 (引用自 PHP.net ):因此使用
fclose($apns);
将关闭 StreamI'm guessing your PHP version may not have the Sockets functionality installed or enabled which is why you are getting that error.
Taken from PHP.net:
But for your Code, you don't need to use that
socket_close($apns)
function. Instead you can just remove it and use the close since (Quote from PHP.net):So using
fclose($apns);
will close the Stream没有关闭套接字的功能,只能创建套接字并通过 fwrite/fclose 等方式管理它:
http://php.net/manual/en/function.stream-socket -client.php
There is no function to close socket, you can only create socket and manage it by fwrite/fclose etc:
http://php.net/manual/en/function.stream-socket-client.php
替换:
with
这应该可以解决您报告的错误:但不确定这是否会真正解决您的 APNS 问题。
Replace:
with
That should solve the error you've reported: not sure if that will actually fix your issue with APNS, though.
我其实已经解决了。问题在于消息的内容(空)。套接字功能完美运行。
I've actually solved. The problem was the content of the message (empty). The socket function works perfectly.
从此网址中删除“沙箱”,它将运行。
Remove "sandbox" from this url and it will run.