我能够从firebase Console将推送通知发送到React-Native中的移动设备,但是当我从php文件发送时,显示成功消息,但在移动设备中没有通知。PHP中的消息是:
{" MultiCast_Id":8573 *********,"成功":1,"失败":0,canonical_ids":0,"结果":[{" Message_id":" 0:" 0:156161518380026 ******************}
用户令牌和服务器密钥是正确的。在先前的项目中,我使用了类似的PHP文件来发送在Android Studio中开发的通知,该通知工作完美。我用于发送通知的PHP代码:
<html>
<head>
<title>ControlPlus Notification Center</title>
</head>
<body>
<center>
<br>
<font size="10" style="bold">ControlPlus Notification Center</font>
<br> <br> <br> <br> <br>
<Table class= "b">
<tr>
<td>
<form method = 'POST' action = '?notifyHealth=1'>
<div>
<input class ='main_button' type = 'submit' value = 'Send Notification'>
</div>
</form>
</td>
</tr>
</Table>
</center>
</body>
</html>
<?php
function sendPushNotification() {
$url = "https://fcm.googleapis.com/fcm/send";
$serverKey = 'AAAA25************************************Xw';
$title = "ControlPlus App";
$body = "New Workorder has been added !! ";
$notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => 'fL8aUT2un*******************************B2bzLa', 'data' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
if(!empty($_GET['notifyHealth'])) {
sendPushNotification();
}
?>
您要发送的JSON有效载荷的结构可能存在问题。根据此处的文档:https://firebase.google.com/docs/cloud-messaging/concept-poptions,应构成如下:
{
"message":{
"token":"fL8aUT2un*******************************B2bzLa",
"notification":{
"title":"ControlPlus App",
"body":"New Workorder has been added !! "
}
}
}