我允許用戶通過我的網站發送通知。通知正在工作,但是,我想要一種方法來實際確認通知是否已發出(在代碼中),或者如果這至少不能確認curl是否有效,那麼我可以在我的網站上顯示一條消息它成功或失敗。在我的jquery post請求中,即使我在我的php中提供了一個無效的API_ACCESS_KEY(因此它顯然沒有發送通知,但它仍然說成功),狀態似乎總是「成功」。我如何確定通知已發出?感謝任何幫助。如何確認Firebase通知實際上已發送(fcm)?
這裏是我的index.html POST請求:
$("#send-button").click(function(){
if($("#send").val().length == 0) {
return;
} else {
$.post("php/send-notification.php",
{
notification_message: $("#send").val()
},
function(data, status) {
alert("Data: " + data + "\nStatus: " + status);
// status seems to always be "success" even with an invalid API_ACCESS_KEY
});
}
});
這裏是發送-notification.php:
<?php
define('API_ACCESS_KEY', 'AAA....AAA');
$msg = array
(
'body' => $_POST['notification_message'],
'vibrate' => 1,
'sound' => 1,
'badge' => 1
);
$fields = array
(
'to' => "/topics/global",
'notification' => $msg,
'priority' => 'high'
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
?>
您是否在任何地方檢查'$ result'的值?你應該檢查HTTP響應代碼,如果它是200,你應該檢查響應主體。 – Eran
我有點困惑這是什麼意思,但是當我嘗試echo $結果時,沒有任何東西(即使通知正在工作併發送到設備)。 –
if($ result = curl_exec($ ch))echo「working」;這似乎總是返回true,即使當我設置一個糟糕的API_ACCESS_KEY –