php - 如何确认 firebase 通知确实已发送(fcm)?

标签 php jquery firebase firebase-cloud-messaging firebase-notifications

我允许用户通过我的网站发送通知。通知正在工作,但是,我想要一种方法来实际确认通知是否已发送(以代码形式),或者如果不可能,至少确认 curl 是否有效,这样我可以在我的网站上显示一条消息它要么成功,要么失败。在我的jquery post请求中,状态似乎总是“成功”,即使我在我的php中提供了无效的API_ACCESS_KEY(所以它显然没有发送通知,但它仍然说成功)。我如何确定通知已发出?感谢您的帮助。

这是我在index.html中的发布请求:

$("#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
        });
    }
});

这里是 send-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 );
?>

最佳答案

您可以使用curl_getinfo

检查响应信息,如果你的状态码200满足一切正常。

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpcode == 200) {
    //everything ok
}

关于php - 如何确认 firebase 通知确实已发送(fcm)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363460/

相关文章:

firebase - 如何在flutter中将用户数据保存在cloud firestore中

php - 表单发送 GET 而不是 POST

javascript - 使用ajax上传图片和表单提交

jquery - 流程图在HTML/CSS中的定位?

swift - Microsoft 通过 Firebase 登录

firebase - Angular Dart firebase电子邮件和密码身份验证

javascript - 使用填充的下拉列表复制 php 表的新行

php - 获取文件上传后在服务器上的客户端路径

php - 是什么原因导致 memcached 错误 10 ('server error' )?

javascript - Chrome 无法识别 24 :00:00 while Firefox does