AabidMulani AabidMulani - 3 months ago 14x
PHP Question

PHP code to send Android push notification to multiple device

Below is the code for php in which i make a curl call to the GCM server:

$i = 0;
$result1 = mysql_query("SELECT * FROM `my_devices`") or die($log->lwrite("Error" . mysql_error()));
while ($row2 = mysql_fetch_array($result1)) {
$i = $i + 1;
$msg = array(
'message' => $resultTextValue

$fields = array(
'registration_ids' => array($row2['gcmId']),
'data' => $msg

$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);

My code is under development and has max of 10 device registered. So it take decent time to make send the notificaiton to all the device.

My concern is: when the app is deployed i will be having thousands of device to send the push notification. Wont it take a big amount of time for the push notification call?

I am new to PHP and copied the above code from some sample application.

Please help me know if i am doing it right. Or else guide me the correct way to do so.

Please help!

Thanks in advance.


first get all the Registration ids to an array

$regIDS = array(); // set variable as array

  // get all ids in while loop and insert it into $regIDS array
 while ($row2 = mysql_fetch_array($result1)) {
array_push($regIDS ,$row2['gcmId'])

then in $fields, mention the array variable name $regIDS instead of array($row2['gcmId']

$fields = array(
        'registration_ids' => $regIDS ,
        'data' => $msg

and you are now able to send msg to multiple device in a single push Notification Message.


example pic=

here i have only two regIDs in database so here Success=2 in only one call of push notification

enter image description here