我有一个连接到Firebase的curl脚本,用于发送推送通知,但问题是通知只发送到一个注册令牌,而应该发送到多个注册令牌(设备(
下面是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);
?>
我注意到的一件事是,您将curl结果存储到变量$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);
}