M.H. Lushan M.H. Lushan - 1 year ago 118
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";

$sql="select fcm_token from fcm_info";

'Authorization:key ='.$server_key,
'Content-Type: application/json'


$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));

$result = curl_exec($ch);

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download