Encountered error while sending firebase notification to topic using curl and api v1
P粉127901279
2023-08-29 17:36:37
<p>在我的 laravel 应用程序中,我使用curl 向订阅某个主题的所有用户发送通知,但是当我发送通知时,我收到此错误:</p>
<pre class="brush:php;toolbar:false;">"code": 400,
"message": "Invalid JSON payload received. Unknown name \"to\": Cannot find field."</pre>
<p>这是我使用curl发送主题notif的方式:</p>
<pre class="brush:php;toolbar:false;">public function sendTopic($topic,$title,$body, $data , $type, $image='')
{
$client = new Client();
$url = 'https://fcm.googleapis.com/v1/projects/wooloveapp-dda64/messages:send';
$fields =
[
'message' =>
[
"to" => $topic,
"notification" =>
[
"title" => $title,
"body" => $body,
],
"data" => [ "data" => json_encode($data) ],
"android" =>
[
"notification" =>
[
"sound" => "default",
"title" => $title,
"body" => $body,
'tag' => $topic,
"channel_id" => "500",
],
"priority" => "high",
"ttl" => "86400s"
//"badge" => 1
],
"apns" =>
[
"payload" =>
[
"aps" => [ "sound" => "default" ]
],
"headers" => [
"apns-priority" => "5",
"content_available" => "1"
],
],
"webpush"=>[
"headers"=>[
"Urgency"=> "high",
//"image" => "https://wooloveapp.com/img/misc/logo-02.jpg"
]
],
]
];
$headers =
[
'Authorization: Bearer ' .$this->getGoogleAccessToken(),
'Accept:application/json',
'Content-Length:'.strlen(json_encode($fields)),
'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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
$result =
[
'result' => $result,
//'statusCode' => $statusCode
];
return $result;
}</pre></p>
You must use
topic
instead ofto
if you are sending to a topic.