【问题标题】:code igniter - Failed to connect: 0 while sending ios notificationscodeigniter - 连接失败:0 发送 ios 通知时
【发布时间】:2019-08-27 11:20:10
【问题描述】:

我正在尝试从 PHP Code Igniter 框架向 iOS APNS 发送远程推送通知。

为此,作为一种实践,我已经编写了核心文件并在同一个帐户上,当我在控制器函数中添加相同的代码时,它运行良好:

“连接失败:0”。

经过大量研究,我知道这条线:

$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

为什么在核心 PHP 中运行相同的代码但在 CI 中却不行?

我尝试给出此处给出的 .pem 文件的完整路径 Failed to connect: 0 push notification

但没有运气。

请帮助我摆脱它变得沮丧。

public function sendIosPushNotifications()
{
        // set time limit to zero in order to avoid timeout
        set_time_limit(0);

        // charset header for output
        header('content-type: text/html; charset: utf-8');

        // this is the pass phrase you defined when creating the key
        $passphrase = '';

        // you can post a variable to this string or edit the message here
        if (!isset($_POST['msg'])) {
        $_POST['msg'] = "Notification message here from gokul sxsxsxs!";
        }

        // tr_to_utf function needed to fix the Turkish characters
        $message = $this->tr_to_utf($_POST['msg']);

        // load your device ids to an array
        $deviceIds = array(
        '6a774e1779d1c1932c112ff7a45c337db96605942acbdb45d5c386a329bda381'
        );

        // this is where you can customize your notification
        $payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';

        $result = 'Start' . '<br />';


        // start to create connection
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates123.pem');
        //stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

        echo count($deviceIds) . ' devices will receive notifications.<br />';

        foreach ($deviceIds as $item) {
            // wait for some time
            sleep(1);

            // 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" . '<br />');
            } else {
                echo 'Apple service is online. ' . '<br />';
            }

            // Build the binary notification
            $msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;

            // Send it to the server
            $result = fwrite($fp, $msg, strlen($msg));

            if (!$result) {
                echo 'Undelivered message count: ' . $item . '<br />';
            } else {
                echo 'Delivered message count: ' . $item . '<br />';
            }

            if ($fp) {
                fclose($fp);
                echo 'The connection has been closed by the client' . '<br />';
            }
        }

        echo count($deviceIds) . ' devices have received notifications.<br />';

        // function for fixing Turkish characters


        // set time limit back to a normal value
        set_time_limit(30);
   }

【问题讨论】:

  • 它托管在linux服务器上,并在那里打开了2195、2196端口......
  • 您的文件 Certificates123.pem 在 CI 应用程序中的什么位置?感觉是路径问题。您可以通过简单地尝试在控制器中的测试方法中打开文件来检查它。
  • 是的,这是路径问题...当我将证书文件放在 CI 根目录中并且它工作时

标签: ios push-notification apple-push-notifications codeigniter-3 apns-php


【解决方案1】:

这是路径问题...将您的证书文件放在 CI 根目录中.. 并提供类似 ./certificate/filename.pem 的路径,它可以工作

【讨论】:

    猜你喜欢
    • 2014-04-04
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多