doumengyin0491 2017-06-05 07:06
浏览 91
已采纳

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

I allow users to send notifications out through my website. The notifications are working, however, I would like a way to actually confirm if the notification was sent out (in code), or if that is not possible at least confirmation that the curl worked, so I can show a message on my website that it either succeeded or failed. In my jquery post request, the status seems to always be "success", even if I provide an invalid API_ACCESS_KEY in my php (so it obviously is not sending the notification yet it is still saying success). How can I know for sure that the notification was sent out? Grateful for any help.

Here is my post request in 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 + "
Status: " + status);
            // status seems to always be "success" even with an invalid API_ACCESS_KEY
        });
    }
});

Here is 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 );
?>
  • 写回答

2条回答 默认 最新

  • duansha6410 2017-06-05 07:25
    关注

    You can use curl_getinfo

    to check response information, if yur status code 200 that meet everything ok.

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($httpcode == 200) {
        //everything ok
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大