0

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;
developer
  • 219
  • 4
  • 14
  • [GCM](https://developers.google.com/cloud-messaging/) is deprecated, please use [FCM](https://firebase.google.com/docs/cloud-messaging/) – Satish Saini Feb 26 '19 at 11:41
  • The technique you need is called *pagination.* – O. Jones Feb 26 '19 at 11:42
  • @O.Jones His question was more about the limit on push notifications can be sent using GCM. Not sure, why did you mark it as duplicate with MySQL pagination stuff. – Satish Saini Feb 26 '19 at 11:46
  • His question was "how do I execute a MySQL query in batches?" That's pagination, whether it's for displaying rows on a page or batches for a messaging system. – O. Jones Feb 26 '19 at 11:48
  • Using FCM, you can send up to 10K notifications at once. Check the [limits](https://firebase.google.com/docs/firestore/quotas). Here is an implementation of FCM using PHP: [How to send FCM notifications using PHP](https://stackoverflow.com/a/42989063/1761380) – Satish Saini Feb 26 '19 at 11:49
  • @O.Jones I agree. He was mixing 2 things in his question. He first got stuck with a limit of notifications can be sent using GCM and that's 1000 as per his statement. He then asked if he can break his db records (~5000 records) in batches so can he call GCM function in loop or create a separate function to call with every 1000 records. We could probably add a good answer for him to clear the problem. – Satish Saini Feb 26 '19 at 11:51

0 Answers0