2

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.

3
  • if you want to send the same message to a certain group of devices, you can use 'registration_ids' (all IDs you want to send the message to) and a message, this way you can send many messages in 1 call. otherwise you may have to send the messages one by one for each recipient. Commented Jan 4, 2015 at 12:20
  • 1
    @Yazan Can you please provide some code for this, i m new to php Commented Jan 4, 2015 at 13:39
  • check this question and the selected answer stackoverflow.com/questions/25859898/… Commented Jan 4, 2015 at 15:06

1 Answer 1

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

what if message is different for each token?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.