php - 需要以1000个为一批发送通知

标签 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 个通知?

最佳答案

希望对你有帮助。

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

谢谢。

关于php - 需要以1000个为一批发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33943807/

相关文章:

php - 将 Laravel 连接到另一个 IP 上的数据库

php - 使用 jquery 在父标签之外附加 html

PHP,文本区域中的 strip_tags 剥离\n。如何阻止它?

java - 将 BufferedImage 转换为 Image 数据类型

mysql - 检索给出最多正确答案的用户名

c# - ASPX 连接 wamp 数据库

mysql - 返回客户列表和平均租金金额 (Sakila)

PHP 正则表达式查找 __ ("STRING_TO_EXTRACT"中包含的字符串)

php - 更改 "label"防御给定类别

objective-c - 优化数据库调用