I want to send firebase notification to all registered token in mysql database for android devices, am using the normal php way of creating array of recipient tokens so that i can send the notification, but when i send notification non of the registered devices receive it. I want to know if there is the need of adding some piece of php codes to work with the firebase so that the job get done, If so how and where should i use it based on my codes or what is wrong with my codes and how can i fix it?
// here is the code for sending notification
<?php
$message =$_POST['message'];
$title=$_POST['title'];
//firebase server url
$path_to_fcm='https://fcm.googleapis.com/fcm/send';
// Here is the server_key
$server_key="##########################";
$conn = mysqli_connect("localhost","db_username","password","database_name");
$sql = "select * from fcm_info";
$result = mysqli_query($conn,$sql);
//HERE IS THE TOKEN
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["fcm_token"];
}
}
/// HERE IS THE KEY SINGLE RECIPIENT IT WORKS FINE WHEN I INTRODUCE TO THE RECIPIENT FIELD("to" field)
$key="############";
$headers= array(
'Authorization:key='.$server_key,
'Content-Type:application/json'
);
// here use $tokens as the replace $key
$fields= array('to'=>$token,
'notification'=>array('title'=>$title,'body'=>$message,
'click_action'=>'com.jrcomjk.firebasemysql_TARGET_NOTIFICATION'
));
$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($conn);
?>