我有一个curl脚本连接到Firebase,以便发送推送通知,但是问题是,当通知应该发送到多个注册令牌(设备)时,通知仅发送到一个注册令牌

下面是CURL代码

<?php

require "init.php";
$body=$_POST['message'];
$title=$_POST['title'];
$url=$_POST['url'];


$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs";

$sql="SELECT * FROM fcm_info";
$result=mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)){
$key=$row[1];

}

$headers=array(

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

);

$message = array(
'title'     => $title,
'body'      => $body,
'webUrl'    => $url,
'vibrate'   => 1,
'sound'      => 1
 );

 $fields = array(

'to'               =>$key,
'data'  => $message,
'priority'=>'high'
 );

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

 mysqli_close($con);

  ?>

最佳答案

我注意到的一件事是您将卷曲结果存储到变量$result中。这与查询结果所在的变量相同,因此替换了它的值。那会引起问题。

试试看。

require 'init.php';
$body = $_POST['message'];
$title = $_POST['title'];
$url = $_POST['url'];


$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs";

$sql="SELECT * FROM fcm_info";
$result = mysqli_query($con, $sql);


$headers = array(

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

);

$message = array(

  'title'     => $title,
  'body'      => $body,
  'webUrl'    => $url,
  'vibrate'   => 1,
  'sound'      => 1

 );

while($row = mysqli_fetch_array($result)){

 $fields = array(

'to'        => $row[1],
'data'      => $message,
'priority'  =>'high'

 );

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

 $curlResults[] = curl_exec($curl_session); //Changed the name of variable so it
 //did not overwrite your query results.

 mysqli_close($con);

}

10-06 14:02
查看更多