如何控制从PHP发送推送到APNS的无响应错误

时间:2016-01-26 18:34:02

标签: php ios apple-push-notifications apns-php

我的实施方面遇到了一些问题,因为在400 - 500次发送后,它会以随机的方式崩溃。

我想知道为什么PHP和其他平台的最佳APNS教程建议您在向APNS服务器发送一些消息后稍微睡一会儿(很多!)。

发送iOS推送通知时为什么要睡眠代码?

我得到的崩溃是

<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 />...

我的源代码:

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

崩溃线:

$fp = stream_socket_client(F3::get('apns_url'), $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

0 个答案:

没有答案