在cURL命令中获取Firebase Cloud Messaging中的未授权错误401

And*_*Dev 3 php curl firebase firebase-cloud-messaging

我将发送Firebase云消息传递,而我遇到的问题是我收到了未经授权的错误401.

我从Firebase网站获得了安全密​​钥,然后进行了设置.设备令牌已经在数据库中,我从数据库读取没有问题.到目前为止这是我的代码:

<?php

function send_notification($tokens, $message)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array('registration_ids' => $tokens, 'data' => $message);
    $headers = array('Authorization: key=' . "My Firebase key", 'Content-Type: application/json');
    echo "work well before init curl";
    $ch = curl_init();
    echo "init well";
    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, $fields);
    echo "init finishe well";
    $result = curl_exec($ch);
    echo "Execute to get result";
    if ($result === false) {
        die('Curl failed: ' . curl_error($ch));
    }
    curl_close($ch);
    echo "function finished well";
    return $result;
}

$conn = mysqli_connect("localhost", "root", "mysql_password", "FCM") or die("Error connecting");

$sql = " Select Token From users";
$result = mysqli_query($conn, $sql);
$tokens = array();
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $tokens[] = $row["Token"];
    }
}
mysqli_close($conn);
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
Run Code Online (Sandbox Code Playgroud)

AL.*_*AL. 6

使用FCM时,必须使用Firebase ConsoleCloud Messaging选项卡中显示的服务器密钥作为请求中的值.Authorization