需要以1000个为一批发送通知
Need to send notification in a batch of 1000
我已经创建了通知应用程序,现在我想向 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 个通知?
类似这样的事情(假设你想在每个 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
}
通过命令行或批处理文件执行PHP脚本:
SET PATH=%PATH%;/FULLPATH/TOPHP/EXECUTABLE
php /FULLPATH/PHPSCRIPT.php 1>execution.log 2>executionError.log
要在 PHP 中使用多个线程,请遵循以下示例:PHP Multithread
要安装 pthread,请遵循 PTHREAD installation tutorial
希望对您有所帮助。
// 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 的完整代码。
谢谢。
我已经创建了通知应用程序,现在我想向 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 个通知?
类似这样的事情(假设你想在每个 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
}
通过命令行或批处理文件执行PHP脚本:
SET PATH=%PATH%;/FULLPATH/TOPHP/EXECUTABLE
php /FULLPATH/PHPSCRIPT.php 1>execution.log 2>executionError.log
要在 PHP 中使用多个线程,请遵循以下示例:PHP Multithread
要安装 pthread,请遵循 PTHREAD installation tutorial
希望对您有所帮助。
// 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 的完整代码。
谢谢。