phppush-notificationapple-push-notificationswindows-server-2008-r2apns-php

APNS not working with Command Prompt


I use a simple php script to send a push notification from my server to my iPhone (see below). I have already imported the certificates. If I open/execute this php-file with browser my iPhone receives the Notification and I receive a positive response from APNS.

Now, I am trying to execute the same php-file with the Command Prompt of my server by using this statement:

php -f C:/inetpub/wwwroot/pushNotification/new.php

I receive the same, positive response from APNS as when I execute the php-file with my browser, but my iPhone never receives the push notification.

Does anyone know what is wrong here?

Thank You!

Björn

php-file:

    <?php       

            $deviceToken ='xxxxx';
            $passphrase = 'mypass';

            $message = "TEST";

            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck-pub.pem');
            stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

            $fp = stream_socket_client(
                'ssl://gateway.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;

            $body['aps'] = array(
                'alert' => $message,
                'sound' => 'default'
                );

            $payload = json_encode($body);

            $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

            $result = fwrite($fp, $msg, strlen($msg));

            if (!$result)
                echo 'Message not delivered' . PHP_EOL;
            else
                echo 'Message successfully delivered' . PHP_EOL;

            fclose($fp);

?>

Command Prompt response:

Connected to APNS
Message successfully delivered

Solution

  • You can try creating a .cmd file with the following lines(ie: test.cmd):

    cd C:\inetpub\wwwroot\pushNotification\
    php -f news.php
    

    And in your file1.php, use this line of code instead:

    exec('test.cmd');
    

    Hope this helps.

    Not tested: You can pass arguments on this way too:

    exec('test.cmd arg1 arg2');
    

    And in the .cmd file

    cd C:\inetpub\wwwroot\pushNotification\
    php -f news.php %1 %2