mysql数据库curl脚本中的php循环fcm令牌

nfeuvbwi  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(387)

我有一个连接到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);

  ?>
0dxa2lsx

0dxa2lsx1#

我注意到的一件事是您正在将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);

}

相关问题