我刚刚在我的实时网络服务器中开发了一个带有应用程序服务器的 firebase 示例。
我在我的本地主机 pc 上试过(离线)并且 firebase 通知是在虚拟设备 android 上发送的。
但是当我尝试将 apk 文件安装到我的手机设备并将 PHP 文件传输到我的实时服务器(即 www.aaa.com)时。
Firebase 通知根本不起作用。它不会发送到我的手机设备。
我是否遗漏了代码中的某些内容? .代码如下。谢谢。
<?php
require "connect.php";
global $con;
if(isset($_POST['Submit'])){
$message = $_POST['message'];
$title = $_POST['title'];
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "SERVER KEY HERE";
echo "Data sent ! <br/>";
$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));
$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);
curl_close($curl_session);
mysqli_close($con);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Send Notification</title>
</head>
<body>
<form action='send_notification.php' method="POST">
<table>
<tr>
<td>Title : </td>
<td><input type="text" name="title" required="required" /></td>
</tr>
<tr>
<td>Message : </td>
<td><input type="text" name="message" required="required" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Send notification"></td>
</tr>
</table>
</form>
</body>
</html>
最佳答案
<?php
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header=array('Content-Type: application/json',
"Authorization: key=YOUR KEY");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"notification\": { \"body\": \"Weather Forecast\",
\"title\": \"YOur title\",
\"to\" : \"YOUR DEVICE REG TOKEN\"}"
curl_exec($ch);
curl_close($ch);
?>
关于php - 将 Firebase Cloud Messaging 发送到手机设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971227/