需要批量发送1000个通知

时间:2015-11-26 16:58:25

标签: php mysql database batch-processing

我已经创建了通知应用程序,现在我想向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发送通知?

3 个答案:

答案 0 :(得分: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;

    }

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

谢谢。

答案 1 :(得分:0)

这样的事情(假设你想要在每个卷曲请求中发送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
}

答案 2 :(得分:0)

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

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

要在 PHP 中午餐多个线程,请按照以下示例操作:PHP Multithread
要安装pthread,请按照此PTHREAD installation tutorial

进行操作