Ahmad Raza Ghausia Malang Ahmad Raza Ghausia Malang - 6 months ago 15
MySQL Question

Sending push messages to all users using GCM

I am trying to send a push message to all android users of my application. I am able to send only to one user and unable to send to all.

Here is my code :


// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'my api key');

$registrationIds = array( 'user_push_id');

$msg = array
'URL' => 'http://www.apple.com/in/',
'message' => 'Did you like it ???',
'image' =>'http://icons.iconarchive.com/icons/dan-wiersma/apple-tv/128/Apple-Logo-icon.png'

$fields = array
'registration_ids' => $registrationIds,
'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 );
echo $result;

please help me getting all the users from mysql table as below:

$sth = mysqli_query("SELECT PUSH_ME from push_msg_android");
$registrationIds = array();
while($r = mysqli_fetch_array($sth)) {
$registrationIds['PUSH_ID'] = $r;


In order to send a push notification to several users, you need to fill the registrationIds array with all users' registration ids.

In the end, it should look like this if you have 3 users:

[78475826748590, 74648598273895, 85737485957849]

In your case, it means replace this line:

$registrationIds = array( 'user_push_id');


$result = mysqli_query($db, "SELECT PUSH_ID from push");
$registrationIds = array();
while($row = mysqli_fetch_array($result)) {
   $registrationIds[] = $row[0];

This is assuming that your users registration ids are stored in the column PUSH_ID of the table push of your database.

In a more normal setting, where your users' registration ids would be stored in a column push_notification_id in a table users, the query should be:

$result = mysqli_query($db, "SELECT push_notification_id from users");
$registrationIds = array();
while($row = mysqli_fetch_array($result)) {
   $registrationIds[] = $row[0];

You will also need to create the $db object before, to create the connection to the database. For example :

$db = mysqli_connect("localhost","my_user","my_password","my_db");