我剛剛在我的實時網絡服務器中開發了一個帶有應用程序服務器的firebase示例。將Firebase Cloud Messaging發送到手機設備
我在本地PC上(離線)嘗試了它,並且在虛擬設備android上發送了firebase通知。
但是,當我嘗試將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>
必須設置服務器密鑰這裏?? –
您嘗試從服務器到虛擬設備還是從本地主機到真實設備?爲了確定問題出在服務器上,不是android設備 – X3Btel
已經在那裏設置了服務器密鑰。 :) ..我只是不想顯示服務器密鑰 – Jamilah