1

I'm having some problems with my implementation because it crash in a random way after 400 - 500 sendings.

I was wondering why the best APNS tutorials for PHP and other platforms advice you to sleep a little (o a lot!) after sending some messages to the APNS server.

Why to sleep your code when sending iOS Push Notifications?

The crash I get is

<html><head><title>500 Internal Server Error</title></head><body>
<h1>Internal Server Error</h1>
<p><i>stream_socket_client(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known</i></p>
<p>#0 /Users/MyUser/development/projects/project/project-api/helpers/PushNotificationHelper.php:407 stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',0,'',60,5,NULL)<br />...

My source code:

static function pushIOS($ios_devices, $push_type, $data_array){

    $log = new Logger('PushConsumer');
    $log->pushHandler(new StreamHandler(BASEPATH.'log/push.log', Logger::DEBUG));

    // set time limit to zero in order to avoid timeout
    set_time_limit(0);

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

    // load your device ids to an array
    $deviceIds = $ios_devices;

    // this is where you can customize your notification
    //payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';
    $msg = array
    (
        'type'          => $push_type,
        'message'       => $data_array,
    );
    $payload = json_encode($msg);
    $result = 'Start' . '<br />';

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

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

    // Open a connection to the APNS server
    $fp = stream_socket_client(F3::get('apns_url'), $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp) {
        $log->addDebug("Failed to connect: $err $errstr");
        exit("Failed to connect: $err $errstr" . '<br />');
    } else {
        $log->addDebug("Apple service is online.");

        foreach ($deviceIds as $item) {

            // 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) {
                $log->addDebug("Undelivered message to push token: $item");
                //echo 'Undelivered message count: ' . $item . '<br />';
            } else {
                $log->addDebug("Delivered message to push token: $item");
                //echo 'Delivered message count: ' . $item . '<br />';
            }

        }


            fclose($fp);
            $log->addDebug("The connection has been closed by the client");

    }

    $log->addDebug(count($deviceIds)." devices have received notifications");

    // wait for some time
    sleep(2);
}

The line with the crash:

$fp = stream_socket_client(F3::get('apns_url'), $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
EnriMR
  • 3,924
  • 5
  • 39
  • 59

0 Answers0