【问题标题】:Unable to send push notifications from Firebase无法从 Firebase 发送推送通知
【发布时间】:2019-07-22 09:14:21
【问题描述】:

我想从网络向 Android 设备发送推送通知,但它响应“无效注册”。

我尝试从 firebase 控制台发送,但不包含消息数据

<?php 

//$con= new mysqli("localhost","root","","doctor_appointment");
include('connection.php');

    $sql = " Select * From token_register where id = 1";

    $result = mysqli_query($connection_to_db,$sql);
    $tokens = array();

    if(mysqli_num_rows($result) > 0 ){

        while ($row = mysqli_fetch_assoc($result)) {
            $tokens[] = $row["token"];
            // $token = $row["token"];
            // echo "$token";
        }
    }

    mysqli_close($connection_to_db);




    function send_notification ($tokens, $message)
    {
        // define( 'API_ACCESS_KEY', 'AIzaSyDt2xaRw4XGzghfxAMRFVy-I8ZeacBDbHA');
        $url = 'https://fcm.googleapis.com/fcm/send';

        $fields = array(
             'registration_ids'  => array($tokens),
             'data'              => array( "message" => $message ),
             );

        $headers = array(
            'Authorization:key = AIzaSyDdHTdvqzN8lrUqhZKOeuiR2d9cETRBhNw',
            'Content-Type: application/json'
            );

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);           
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
    }
    $message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
    $message_status = send_notification($tokens, $message);
    echo $message_status;






 ?>

我希望是

{
    "multicast_id":7068539387015084016,
    "success":1,
    "failure":0,
    "results":[
        {"RegistrationSuccess"}
    ]
}  

但响应

{
    "multicast_id":7068539387015084016,
    "success":0,
    "failure":1,
    "canonical_ids":0,
    "results":[
        {
            "error":"InvalidRegistration"
        }
    ]
}

【问题讨论】:

    标签: php firebase firebase-cloud-messaging


    【解决方案1】:

    根据您给定的日志,注册 ID 是错误的。

    尝试通过 Rest API 工具发送。例如。邮递员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-05
      • 2019-11-11
      • 2019-03-18
      • 2018-11-10
      • 2017-11-23
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多