I am having this script to send a push notification to a single user.
I want to make a loop in the script to send the notification to ALL users.
I must get the tokens from the database and loop for each one.
<?php
require "init.php";
$message=$_POST['message'];
$title=$_POST['title'];
$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="MY_SERVER_KEY";
$sql="select * from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_row($result);
$key=$row[0];
$headers= array(
'Authorization:key='.$server_key,
'Content-Type:application/json'
);
$fields= array('to'=>$key,
'notification'=>array('title'=>$title,'body'=>$message));
$payload=json_encode($fields);
$curl_session= curl_init();
curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session,CURLOPT_POST, true);
curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload);
$result=curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($con);?>
What I have tried is:
while($d=mysqli_fetch_array($result,MYSQLI_BOTH))
{
echo $d[0];
}
and I changed:
$key to $d[0];
But it didn't work,
Any suggestions, please.
//Updated
Check it: Result
//Riggs is this my update:
I used to test your code and no error has shown up.
I added echo $row['fcm_token']; after the while loop but the page is not displaying my echo.
The notification has been sent to the first row of fcm_token in the database. I think Riggs that the loop isn't fetching the second row and it isn't setting the $key
to the new token.
Can u have a way to simply do:
//do same steps
My guess would be that fcm_token
is not the first column returned in the query result, when using SELECT *
When you query a database using the *
i.e. return all columns there is no reason why the single column you want will be the first column returned in the result array. So it is dangerous to assume that by using $row[0]
Instead specify the column name in the query if you only want specific columns returned, not only do you then know which column will be the first/second/... in the result array, it is also more efficient.
$sql="select fcm_token from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_row($result);
// now using columns positions i.e. $row[0] will be getting the fcm_token col
$key=$row[0];
Better still use mysqli_fetch_assoc()
and the name of the column like
$sql="select fcm_token from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($result);
$key=$row['fcm_token'];
When it comes to your loop over the multiple results returned, again use a column name and use the parameter MYSQLI_ASSOC so the row gets returned as an associative array i.e. the column names are used as the key to the row array.
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo $row['fcm_token];
}
Or
while($row=mysqli_fetch_assoc($result))
{
echo $row['fcm_token];
}
RE: You comment, is this what you are suggesting you want to do
<?php
require "init.php";
$message=$_POST['message'];
$title=$_POST['title'];
$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="MY_SERVER_KEY";
$sql="select * from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);
// remove this it is fetching the first row outside the loop
//$row=mysqli_fetch_row($result);
//$key=$row[0];
$headers= array(
'Authorization:key='.$server_key,
'Content-Type:application/json'
);
while($row=mysqli_fetch_assoc($result)) {
$fields = array('to'=>$row['fcm_token'],
'notification'=>array(
'title'=>$title,
'body'=>$message
)
);
$payload = json_encode($fields);
$curl_session= curl_init();
curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session,CURLOPT_POST, true);
curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload);
$result=curl_exec($curl_session);
curl_close($curl_session);
}