我允许用户通过我的网站发送通知了。 该通知的工作,但是,我想一个办法,如果通知已发出(代码)实际确认,或者如果这是不可能的,至少确认卷曲的工作,这样我就可以显示在我的网站的消息它要么成功或失败。 在我的jQuery POST请求,状态似乎永远是“成功”,即使我在我的PHP提供了一个无效API_ACCESS_KEY(所以它显然没有发出通知,但它仍然是说成功)。 我怎么能知道肯定通知已发送出去? 感谢您的帮助。
这里是我的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 );
?>