【问题标题】:Need to send notification in a batch of 1000需要批量发送通知 1000
【发布时间】:2015-11-26 16:58:24
【问题描述】:

我已经创建了通知应用程序,现在我想向 1000 个用户发送通知,但由于我是新手,我不知道如何创建该批处理。

这是我推送通知的代码。

<?php
include('header.php');

define( 'API_ACCESS_KEY', '[API-KEY comes here]' );

$sql="SELECT * FROM user_data";
$result = mysqli_query($conn, $sql) or die ('Error'.mysqli_error($conn));
$registrationIds=array();
while($row=mysqli_fetch_assoc($result)){

    $registrationIds[] = $row['allow'];
}
$ids=json_encode($registrationIds);
$fields = array
(
    'registration_ids'  => $registrationIds
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;


?>

现在我可以向用户推送通知,但是如果我有 10000 个用户,我如何以 1000 个批量发送通知?

【问题讨论】:

  • 到目前为止你尝试过什么?你被困在哪里了?为什么不写一个循环,批量发送通知API调用呢?

标签: php mysql database batch-processing


【解决方案1】:

希望对你有帮助。

    // Chunk of Array with 999 Records, I have not taken risk for 1000 users :D
    // Here $array is a collection of rows which you get from Table.
    $final_array = array_chunk($array, 999);

    // Loop for Every Sub Array and Send to FCM Server
    foreach($final_array as $array_of_chunk) {

        // Your code for sending notification...
        // $registrationIds = $array_of_chunk;

    }

您可以在此处查看 PHP 的完整代码 Send Firebase Notification to more than 1000 users at a time from PHP

谢谢。

【讨论】:

    【解决方案2】:

    类似这样的事情(假设您想在每个 curl 请求中发送一批 1000 个注册 ID):

    $batch_size = 1000; // 1000 per request
    
    $iterations = ceil(count($registrationIds) / $batch_size); // determine how many iterations are needed
    
    for ($i = 0; $i < $iterations; $i++) {
        $offset = $i * $batch_size;
        $batch_of_1000_user_ids = array_slice($registrationIds, $offset, $batch_size);
    
        // json_encode, prepare headers and send curl request
    }
    

    【讨论】:

      【解决方案3】:

      通过命令行或批处理文件执行 PHP 脚本:

      SET PATH=%PATH%;/FULLPATH/TOPHP/EXECUTABLE
      php /FULLPATH/PHPSCRIPT.php 1>execution.log 2>executionError.log
      

      要在 PHP 中使用多个线程,请按照以下示例进行操作:PHP Multithread
      要安装 pthread,请关注 PTHREAD installation tutorial

      【讨论】:

      • 请分享更多细节。这个答案如何确保批量发送 1000 个通知调用?
      猜你喜欢
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多