I've developed an app which has php as its backend support. The app has the capability of receiving push notifications and its working perfectly. But the issue I'm currently facing is GCM allows 1000 messages to be pushed to devices at a time. Meaning if you select more than 1000 device IDs from the database table, GCM wail fail. How do i execute my query in batches so then if I have like 5000 device IDs in my database table, i could send 1000 at a time and after send another 1000 still it reaches 5000.
$stmt = $conn->query("SELECT device_id from devices_tbl");
$gcmRegIds = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
array_push($gcmRegIds, $row['device_id']);
}
$apiKey = "AIzaXXXXXXmVUMNd_XXXXXXXohkT4";
$registrationIDs = $gcmRegIds;
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( 'message' => 'Hello there','title' => "Test Notification",'notId'=>''.time(), 'display'=>'news','badge'=>'1'),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the URL, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;