php - 苹果推送通知..被拒绝?

标签 php ios apple-push-notifications

我需要用 php 服务器实现 apple 推送通知。我认为连接正常工作,但苹果服务器出于某种原因拒绝了该消息。

我收到这个错误:

"error 0errorString
\nFatal error: Call to undefined function socket_close() in /home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php on line 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";

谢谢

最佳答案

我猜您的 PHP 版本可能没有安装或启用套接字功能,这就是您收到该错误的原因。

取自PHP.net :

The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the --enable-sockets option to configure.

但是对于您的代码,您不需要使用那个socket_close($apns) 函数。相反,您可以删除它并使用 close since ( Quote from PHP.net ):

On success a stream resource is returned which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()), FALSE on failure.

所以使用 fclose($apns); 将关闭 Stream

关于php - 苹果推送通知..被拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419931/

相关文章:

ios - apns-collapse-id 不合并多个通知 iOS 10

php - iPhone 收不到推送通知

php - 在 PHP 中获取对象的引用计数?

php - 专门将YouTube网址转换为iframe代码,同时将BBCode网址转换为html元素

php - Laravel 验证 'exist' 在主从数据库配置中不起作用?

iOS Multipeer Connectivity - 在自定义 MCNearbyServiceBrowser 中显示 DiscoveryInfo

javascript - iOS Safari/Chrome 中的 Cookie 持久性

php - zend 框架 - sql 查询的结果数组可以包含数据库名称吗?

ios - iOS:应用程序语言独立于设备,无需更改NSUserDefaults

ios - 像 WhatsApp 一样替换推送/本地通知