如何确认实际发送了 firebase 通知 (fcm)?

How can I confirm that firebase notification was actually sent (fcm)?

我允许用户通过我的网站发送通知。通知有效,但是,我想要一种方法来实际确认通知是否已发送(以代码形式),或者如果不可能至少确认 curl 有效,这样我就可以在我的网站上显示一条消息它要么成功要么失败。在我的 jquery post 请求中,状态似乎总是 "success",即使我在 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 );
?>

您可以使用curl_getinfo

查看响应信息,如果你的状态码满足200就一切ok了。

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

您将获得 Success 结果 $result = curl_exec( $ch ); 结果格式将为

 "multicast_id": 6581315937669460028,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1495111364345221%d8a1cb15f9fd7ecd"
    }
  ]
}