Firebase FCM - 411 错误。 POST 请求需要 Content-length 标头。这就是我们所知道的

问题描述 投票:0回答:0

我正在尝试使用 FCM 向设备发送通知。在服务器端,我通过 python 中的 FCM Admin SDK (

$auth_token
) 和设备
$token
获得了授权令牌。在运行代码时,我得到以下功能:

function sendNotification($token, $title,$body){
    
        // Get the Authorisation Token
    $command = escapeshellcmd('/usr/bin/python3 api/fcm.py');
    $auth_token = shell_exec($command); 

        // set request data
    $request_data = array(
        'message' => array(
        'token' => $token,
        'notification' => array(
            'body' => 'This is an FCM notification message!',
            'title' => 'FCM Message'
            )
        )
    );

        $request_length = strlen(json_encode($request_data));
    
    // set headers
    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $auth_token,
        'Content-Length: '. $request_length
    );

    // set cURL options
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/v1/projects/black-queen-8eff3/messages:send');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, json_encode(null));
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

    // execute request
    $response = curl_exec($ch);

    // check for errors
    if(curl_errno($ch)) {
        print('Error: ' . curl_error($ch));
    }

    // close cURL session
    curl_close($ch);

    // print response
    print($response);
}

但是,我收到以下错误:“411。这是一个错误。POST 请求需要内容长度标头。这就是我们所知道的。”

php curl post firebase-cloud-messaging content-length
© www.soinside.com 2019 - 2024. All rights reserved.