Need to send notification in a batch of 1000

本秂侑毒 提交于 2019-12-02 07:27:20

问题


I have created notification application , now i want to send notification to batch of 1000 users, but as i am new i dont know how to create that batch.

this is my code to push notification.

<?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;


?>

Now i can push notification on to users but how can i send notification in batch of 1000 if i have 10000 users?


回答1:


Something like this (assuming that you want to send batch of 1000 registration ids with every curl request):

$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:


Hope it will helps you.

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

    }

You can see full code for PHP here Send Firebase Notification to more than 1000 users at a time from PHP.

Thank you.




回答3:


To execute a PHP script via command line or from batch file :

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

To lunch multiple threads in PHP, follow this example :PHP Multithread
To install pthread, follow this PTHREAD installation tutorial



来源:https://stackoverflow.com/questions/33943807/need-to-send-notification-in-a-batch-of-1000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!