M.H. Lushan M.H. Lushan - 22 days ago 7
MySQL Question

Firebase Push Notification From PHP MYSQL Server Doesn't Work on Multiple Device

I was trying to send notification on Android devices using MySql PHP Server and Firebase. Firebase tokens are retrieved successfully on my DB but when I try to send notification to all devices from php, it send to the first device saved on my db.

I am here posting the PHP code, I analyzed it a bit but the code is done for one firebase token.

function sendPushNotification() {
require "init.php";
$message="Notification Details";
$title="Notification Title";

$url='https://fcm.googleapis.com/fcm/send';
$server_key="MY_FIREBASE_KEY";
$sql="select fcm_token from fcm_info";
$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));

$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

$result = curl_exec($ch);
curl_close($curl_session);
mysqli_close($con);}


Later I tried changing a little bit, querying all tokens from DB and making assoc array and then pass that array as the key. In that case notification doesn't send and gives me an error saying

"to" must be json

Answer

As @Selvin said, I solved my problem using registration_ids instead of to .