我正在尝试创建一个带有通知功能的应用程序。通知必须从PHP文件发送到Firebase,然后再推送到设备上。一开始,我尝试使用Firebase控制台发送通知,它可以完美运行。但是当我尝试使用PHP发送通知时就遇到了问题。它显示通知已经成功发送,但我并没有收到任何通知。这是我的PHP代码:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = my api key',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$data = array("alert" => "How it is going today");
$token = "fJVM7YrWGQg......";
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($token, $message);
echo $message_status;
我已经搜索了解决方法,但是没有什么有用的。