Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistent connection via PHP to APNS

I know that there are many posts on SO that address this problem, unfortunately I'm not that advanced in PHP programming and I have a question that hasn't been answered somewhere else:

Many of the tutorials for Apple Push Notifications create a connection via stream_socket_client(). But most of them are lacking the flag "STREAM_CLIENT_PERSISTENT". Would this flag make the connection really persistent? If so when would it be closed? The documentation says it will stay connected also on page reloads. Is this depending on sessions?

The version without this flag is working but I'm afraid the APNS will block me as soon as I put in the production certificates etc. (described here). Thanks in advance.

like image 895
Quxflux Avatar asked Jun 08 '12 10:06

Quxflux


1 Answers

As per the PHP documentation on Predefined Constants, using STREAM_CLIENT_PERSISTENT with the APNS connection should keep the connection active between page loads. This is a requirement for the APNS connection, as it WILL throttle you as it considers any disconnection after sending a payload a potential Denial of Service attack.

Should you have any problems with the client outside of the presisitent connections, you might want to try the following, as it's the best way I've seen to handle an APNS connection thus far within PHP. This uses the client from PHPXMLRPC, so you will have to download that package.

<?php

include '../vendors/xmlrpc.inc';

$hostName = 'localhost'; # Your services endpoint here.
$rpcPath = '';
$port = 7077;

if($_GET['action'] == 'provisioning')
{
    $echoString = new xmlrpcmsg(
        'provision',
        array(
            php_xmlrpc_encode('appid'),
            php_xmlrpc_encode('/path/to/certificate.pem'),
            php_xmlrpc_encode('sandbox'),
            php_xmlrpc_encode(100)
        )
    );
    $continue = TRUE;
}

if($_GET['action'] == 'notify')
{
    $echoString = new xmlrpcmsg(
        'notify',
        array(
            php_xmlrpc_encode('paparazzme'),
            php_xmlrpc_encode(array('6bcda...', '7c008...')),
            php_xmlrpc_encode(array(array("aps" => array("alert" => "Hello User 1" )), array("aps" => array("alert" => "Hello User 2" ))))
        )
    );
    $continue = TRUE;
}

if($continue == true)
{
    # Create a client handle and send request
    $client = new xmlrpc_client($rpcPath, $hostName, $port);

    # A little verbose debug
    $client->setDebug(2);

    # The response
    $response = &$client->send($echoString);

    # Check if response is good
    if (! $response->faultCode())
        print "\nReturned string is: " . php_xmlrpc_decode($response->value()) . "\n";
    else
        print "An error occurred: \nCode: " . $response->faultCode() . " Reason: '" . htmlspecialchars($response->faultString()) . "'\n";
}

?>

SOURCE: How to get started with APNS for iPhone or iTouch

I would like to take the time, to point out that, I've not tested any of this code, I do not have an iPhone application right now to test this with, so I can tell you if this actually works.

If it is feasable for you, I would recomend that you use Uban Airship instead, as they do provide 250,000 free pushes a month to each of their clients, and that handle the connection to the APN Server for you, from there you use their APIs to talk to your clients.

like image 81
Mark Tomlin Avatar answered Oct 28 '22 11:10

Mark Tomlin