【问题标题】:Pushing notifications using ApnsPHP is slow使用 ApnsPHP 推送通知很慢
【发布时间】:2015-07-11 15:10:33
【问题描述】:

我正在为我的项目使用 ApnsPHP 库向我的用户发送推送通知:

private static function fnSendToIos($tokens, $text, $config)
{
    set_time_limit(200);

    // Adjust to your timezone
    date_default_timezone_set('Europe/Moscow');

    // Using Autoload all classes are loaded on-demand
    require_once 'ApnsPHP/Autoload.php';
    // Instantiate a new ApnsPHP_Push object
    $push = new ApnsPHP_Push(
        ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
        $config['sert']
    );

    // Set the Root Certificate Autority to verify the Apple remote peer
    $push->setRootCertificationAuthority($config['RootCertificat']);

    // Connect to the Apple Push Notification Service
    $push->connect();

    // Instantiate a new Message with a single recipient
    $message = new ApnsPHP_Message();

    //Put all tokens as recipients in one message
    foreach ($tokens as $token) {
        $message->addRecipient($token);
    }
    // Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
    // over a ApnsPHP_Message object retrieved with the getErrors() message.
    $message->setCustomIdentifier("Message-Badge-1");
    // Set badge icon to "1"
    $message->setBadge(1);
    // Play the default sound
    $message->setSound();
    // Set a simple welcome text
    $message->setText($text);
    // Add the message to the message queue
    $push->add($message);

    // Send all messages in the message queue
    $push->send();
    // Disconnect from the Apple Push Notification Service
    $push->disconnect();

    // Examine the error message container
    $aErrorQueue = $push->getErrors();
    if (!empty($aErrorQueue)) {
        log2file('iosPushNotifications', 'Send push notifications err:' . print_r($aErrorQueue));
    }
}

但它的工作速度很慢。第一条消息运行得很快,但每条下一条都需要 1 分钟或更长时间。 日志文件:

2015-05-01 17:58:30  UTC  192.168.2.1 a3d05958a5f7acdede098a2369ea7782
INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...

2015-05-01 17:58:31  UTC  192.168.2.1 749386767ecdb42b51961dc90cfce4c4
INFO: Connected to ssl://gateway.sandbox.push.apple.com:2195.

2015-05-01 17:58:31  UTC  192.168.2.1 aacaa098ebce25abd2848d37962f0ae8
INFO: Sending messages queue, run #1: 2 message(s) left in queue.

2015-05-01 17:58:31  UTC  192.168.2.1 bde685d96b4309af2d9b8d592576f48d
STATUS: Sending message ID 1 [custom identifier: Message-Badge-1] (1/3): 150 bytes.

2015-05-01 17:59:31  UTC  192.168.2.1 1bdbc022e1037b8be36e40bb883c364c
STATUS: Sending message ID 2 [custom identifier: Message-Badge-1] (1/3): 150 bytes.

2015-05-01 18:00:32  UTC  192.168.2.1 7a81d80022327339521d4ba54b64c4cf
INFO: Disconnected.

消息之间的时间过长。怎么了?

【问题讨论】:

  • 您的服务器在地理位置上位于何处?它的网络速度是多少?如果您尝试将 curl 请求发送到另一台服务器(大概在美国),它是否同样慢?
  • 互联网运行速度很快。 Android pushNotifications 也很快。
  • 您知道要向其发送请求的服务器的地址吗?如果是这样,您的服务器上的 traceroute/ping 测试显示了什么?
  • 不,下面的代码可以正常工作。 Tnx。

标签: php notifications apple-push-notifications push


【解决方案1】:

由于库在每次发送消息后都在等待来自 APNS 服务器的响应而导致延迟一分钟。 我找到了一个不使用 ApnsPHP 库的解决方案。有了错误处理程序和反馈,它就可以在我的情况下使用。

对我有用的代码(也许对某人有帮助):

/**
*@param array $tokens - device tokens
*@param string $text - push notification message text
*@param array $config - contains paths to certificates 
*/   
private function fnSendIosUseSockets($tokens, $text, $config)
        {
            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', $config['sert']);
            stream_context_set_option($ctx, 'ssl', 'passphrase', '');

            $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
                $err,
                $errstr,
                60,
                STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
                $ctx);

            if (!$fp) {
                log2file('iosPushNotifications', "Filed to connect push.apple.com. Err: $err $errstr " . PHP_EOL);
               return false; //exit
            }

            echo 'Connected to APNS' . PHP_EOL;

            foreach($tokens as $token) {

                // Create the payload body
                $body['aps'] = array(
                    'badge' => +1,
                    'alert' => $text,
                    'sound' => 'default'
                );

                $payload = json_encode($body);

                // Build the binary notification
                $msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;

                // Send it to the server
                $result = fwrite($fp, $msg, strlen($msg));

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

            // Close the connection to the server
            fclose($fp);
            echo "Disconnect.";
        }

【讨论】:

  • 这对我有用。我只是将网址更改为'tls://gateway.sandbox.push.apple.com:2195',因为它可能是/变成mandatory
猜你喜欢
  • 1970-01-01
  • 2014-04-29
  • 2018-09-25
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2011-12-12
相关资源
最近更新 更多