This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Symfony2 and APNS iOS

Discuss installing Symfony 2 here

Symfony2 and APNS iOS

by Terry » Thu Oct 03, 2013 9:26 pm

Hi friends developers,

I developed a web application in Symfony2 which incorporate an API for interacting with one of my mobile applications on iOS. To do simple, my web application send daily push notifications via the APNS service Apple on the mobile application. For this I use the following function:
Code: Select all
<?php

function sendNotification($message, $numBadge, $deviceTokenList)
{
    $body = array();
        $body['aps'] = array();
        $body['aps']['alert'] = $message;
        $body['aps']['badge'] = $numBadge;
        $body['aps']['sound'] = "default";
        $payload = json_encode($body);

        // Device token
        $deviceToken = "";

        // Create SSL context
        $ctx = stream_context_create();

        // Opening PEM file
        $pathCk = $this->kernel->locateResource('@MyBundle/Notifications/ck.pem');
        stream_context_set_option($ctx, 'ssl', 'local_cert', $pathCk);

        // Opening connection with apple servers
        $fp = stream_socket_client('ssl://gateway.push.apple.com:2195',
        $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

        if(!$fp) {
            print "Connection error: $err $errstr\n";
        }
        else {
            print "Connection OK\n";
        }

        // For each device registered
        foreach($deviceTokenList as $deviceToken)
        {
            // Construction des données à envoyer
            $msg = chr(0) . pack('n', 32)
            . pack('H*', str_replace(' ', '', $deviceToken))
            . pack('n', strlen($payload)) . $payload;

            print "Message: " . $payload . "\n";

            // Sending data
            $result = fwrite($fp, $msg, strlen($msg));

            if (!$result){
            echo '<p>The message has not been sent '. PHP_EOL;

            }
            else{
            echo '<p>The message has been sent ' . PHP_EOL;

            }
        }

        // Close connection
        fclose($fp);
}

?>

This function works without Symfony2 as before migrating under this Framework the notification system was integrated into another web application. To try to make it work in Symfony2 so I added the line:
Code: Select all
<?php
        $pathCk = $this->kernel->locateResource('@MonBundle/Notifications/ck.pem');
?>

and replace this line:
Code: Select all
<?php
    stream_context_set_option($ctx, 'ssl', 'local_cert', './ck.pem');
?>

by the following:
Code: Select all
<?php
    stream_context_set_option($ctx, 'ssl', 'local_cert', $pathCk);
?>

So, the problem is at the opening and the transmission of the certificate ck.pem to Apple Service: either it is wrong open or not.

I hope you understand and you can help me.
Thanks a lot.
Terry
Junior Member
 
Posts: 2
Joined: Thu Oct 03, 2013 9:22 pm

Re: Symfony2 and APNS iOS

by Terry » Sat Oct 05, 2013 10:52 am

I have a bit more to isolate the problem :
The connection fails when I call the service from the console but it works well if I call from a controller.

Have any ideas?
Terry
Junior Member
 
Posts: 2
Joined: Thu Oct 03, 2013 9:22 pm