php - 发送 APNS 千台设备 php 花费太多时间

标签 php apple-push-notifications

我在循环中用 PHP 向多个设备发送 APNS。

while($row = mysql_fetch_array($result))
  {
    $row['devicetoken'];
    $row['devcertificate'];
    $row['prodcertificate'];

    if($devprod=="dev"){
        $apnsserverurl="ssl://gateway.sandbox.push.apple.com:2195";
        $certificatename=$appname."".$row['devcertificate'];
    }
    elseif($devprod=="prod"){
        $apnsserverurl="ssl://gateway.push.apple.com:2195";
        $certificatename=$appname."".$row['prodcertificate'];
    }
    sendpush($row['devicetoken'],$certificatename,$apnsserverurl);
  }

这里是发送推送函数:

function sendpush($deviceToken,$certificatename,$apnsserverurl){
// Get the parameters from http get or from command line
$message = utf8_encode(urldecode($_POST['message']));
$badge = 0;
$sound = "";
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */



$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $certificatename);

$fp = stream_socket_client($apnsserverurl, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
    //print "Failed to connect $err";
    return;
}
else {
    //print "Connection OK";
}
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "n";
fwrite($fp, $msg);
fclose($fp);   
}

我面临的问题是它花费了太多时间。如何优化代码?


我终于使用了 https://code.google.com/p/apns-php/ , 仅建立 1 个连接并对消息进行排队

最佳答案

APNs 最佳实践 要求您保持与他们服务器的连接打开:

You may establish multiple connections to the same gateway or to multiple gateway instances. If you need to send a large number of push notifications, spread them out over connections to several different gateways. This improves performance compared to using a single connection: it lets you send the push notifications faster, and it lets APNs deliver them faster.

Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.

Source

Apple 可能将您的重复连接视为 DoS 攻击并限制处理。

关于php - 发送 APNS 千台设备 php 花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19115897/

相关文章:

php - mysql 删除/更新或在代码中处理(例如 PHP)

php - Laravel 4 - 使用多个复选框重新填充表单以进行编辑

ios - Mixpanel 设备 token 消失

javascript - Laravel:通过 min.js 文件 "Uncaught ReferenceError: HelloWeek is not defined"包含 javascript 代码

php - 如何添加对数据进行排序的按钮或链接?

ios - 退出应用程序时从远程通知显示 uiviewcontroller

Azure 通知中心 : How to view per message telemetry?

iphone - 设置推送通知的服务器端

iphone - OSX Keychain Access - 从现有的 APNS 私钥生成 CSR(Apple 推送通知服务)

php - Handlebarsjs PHP 渲染