我想向我的数据库中的所有用户发送推送通知,但是使用此代码我只向我的数据库中的最后一个用户发送通知。
<?php
$connect = mysqli_connect("localhost", "***", "***", "***");
$message = $_POST["message"];
$title = $_POST["title"];
$path = "https://fcm.googleapis.com/fcm/send";
$server_key = "***";
$sql = "SELECT fcm_token FROM User";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_assoc($result)) {
$headers = array("Authorization:key=" .$server_key, "Content-Type:application/json");
$fields = array("to"=>$row["fcm_token"], "notification"=>array("title"=>$title, "body"=>$message));
$payload = json_encode($fields);
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path);
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_IFRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
}
$result = curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($connect);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
有人知道解决方案吗?
答案 0 :(得分:0)
您需要在while循环中执行您的请求。因此,您需要以下列方式重写代码:
<?php
$connect = mysqli_connect("localhost", "***", "***", "***");
$message = $_POST["message"];
$title = $_POST["title"];
$path = "https://fcm.googleapis.com/fcm/send";
$server_key = "***";
$sql = "SELECT fcm_token FROM User";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_assoc($result)) {
$headers = array("Authorization:key=" .$server_key, "Content-Type:application/json");
$fields = array("to"=>$row["fcm_token"], "notification"=>array("title"=>$title, "body"=>$message));
$payload = json_encode($fields);
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path);
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_IFRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$curl_result = curl_exec($curl_session);
curl_close($curl_session);
}
mysqli_close($connect);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
答案 1 :(得分:0)
最好收集所有FCM令牌并使用registration_ids
确保只向FCM服务器发送一个HTTP请求。您可以找到完整的FCM HTTP协议here。
<?php
$connect = mysqli_connect("localhost", "***", "***", "***");
$message = $_POST["message"];
$title = $_POST["title"];
$path = "https://fcm.googleapis.com/fcm/send";
$server_key = "***";
$sql = "SELECT fcm_token FROM User";
$result = mysqli_query($connect, $sql);
$tokens = array();
while($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["fcm_token"];
}
$headers = array("Authorization:key=" . $server_key, "Content-Type:application/json");
$fields = array(
"registration_ids" => $tokens,
"notification" => array("title" => $title, "body" => $message)
);
$payload = json_encode($fields);
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path);
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_IFRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$curl_result = curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($connect);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>