drqxfmfa804578 2016-11-04 10:40
浏览 54
已采纳

来自PHP MYSQL Server的Firebase推送通知在多个设备上不起作用

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

  • 写回答

1条回答 默认 最新

  • duanbo7517 2016-11-15 13:34
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改