【发布时间】:2016-12-06 01:55:37
【问题描述】:
我尝试使用 PHP 代码/网络浏览器发送 FCM。
但问题是当我使用 PHP 网络浏览器发送它时:
- FCM 通知仅显示在虚拟设备上。
- FCM 通知不会出现在真实的手机设备上。
而且我只能使用 Firebase 控制台向真实手机设备发送 FCM 通知。
有人可以帮忙吗?代码如下。
<?php
require "init.php";
global $con;
if(isset($_POST['Submit'])){
$message = $_POST['message'];
$title = $_POST['title'];
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "AAAA2gV_U_I:APA91bHA28EUGmA3BrDXFInGy-snx8wW6eZ_RUE7EtOyM99pbfrVZU_ME-FU0O9_dUxYpM30OYF8KWYlixod_PfwbgLNoovzdkdJ4F-30vY8X_tBz0CMrajCIAgbNVRfw203YdRGli";
$sql = "SELECT fcm_token FROM fcm_table";
$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>FCM Notification</title>
</head>
<body>
<form action='fcm_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,对吧?
-
是的。安卓设备
-
您能否正确获取模拟器的注册令牌?您是否尝试过使用简单的 cURL 响应发送消息并查看 iut 是否仍然没有收到任何消息?
-
是的。注册令牌从模拟器工作到数据库。
-
你能检查一下这个link.
标签: php android curl firebase firebase-cloud-messaging