我正在尝试使用FCM向设备发送通知。在服务器端,我通过python中的FCM Admin SDK($auth_token
)和设备$token
获得了授权令牌。在运行代码时,我得到以下函数:
function sendNotification($token, $title,$body){
$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' => $body,
'title' => $title
)
)
);
$fields = json_encode($request_data);
$request_length = strlen($fields);
print($request_length);
// set headers
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $auth_token,
'Content-Length: '. $request_length
);
$url = 'https://fcm.googleapis.com/v1/projects/black-queen/8eff3/messages:send';
$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_POSTFIELDS, $fields );
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$result = curl_exec ( $ch );
echo $result;
// check for errors
if(curl_errno($ch)) {
print('Error: ' . curl_error($ch));
}
// close cURL session
curl_close($ch);
// print response
print($response);
}
字符串
但是,我收到以下错误消息:这是一个错误。POST请求需要Content-length报头。我们只知道这些。”
1条答案
按热度按时间a14dhokn1#
如果其他人也面临这个问题,我这边的问题是白色。我把
trim()
放在所有头输入的周围(特别是对我来说是$auth_token
),这解决了它