Reputation: 2325
Below is the code for php in which i make a curl call to the GCM server:
//CODE TO SEND PUSH NOTIFICATION
define('API_ACCESS_KEY', 'XXXXXXXXXXXXXXXXXXXXX');
$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);
curl_close($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. Won't 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.
Upvotes: 2
Views: 4165
Reputation: 5535
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.
http://php.net/manual/en/function.array-push.php
example pic=
here i have only two regIDs in database so here Success=2 in only one call of push notification
Upvotes: 1