我试图在Android设备上使用MySql PHP服务器和Firebase发送通知。Firebase令牌在我的DB上成功检索,但当我尝试从php向所有设备发送通知时,它发送到保存在我的DB上的第一个设备。
我在这里发布PHP代码,我分析了一下,但代码是为一个firebase令牌完成的。
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);}
后来我试着改变一点,从DB查询所有令牌,并使关联数组,然后传递该数组作为键。在这种情况下,通知不会发送并给我一个错误,说
"to" must be json
正如@Selvin所说,我用registration_ids
代替to
解决了我的问题。