Search code examples
iospush-notificationapple-push-notificationscodeigniter-3apns-php

code igniter - Failed to connect: 0 while sending ios notifications


I am trying to send remote push notification to iOS APNS from PHP Code Igniter framework.

for that as a practice i had written core file and on the same account and its running fine when i added the same code in controller function it gives me:

"Failed to connect: 0".

after lots of research I come to know that the line:

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

Why it is like same code is working in core PHP but not in CI?

I tried giving the full path of my .pem file as given here Failed to connect: 0 push notification

but no luck.

please help me to get rid of this its getting frustrated.

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);
   }

Solution

  • It was path issue... place your certificate file in CI root directory.. and give the path like ./certificate/filename.pem and it worked