无法从Firebase发送推送通知

时间:2019-07-22 09:14:21

标签: php firebase firebase-cloud-messaging

我想将推送通知从Web发送到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"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

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

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