首页  >  问答  >  正文

使用curl和api v1向主题发送firebase通知时遇到错误

<p>在我的 laravel 应用程序中,我使用curl 向订阅某个主题的所有用户发送通知,但是当我发送通知时,我收到此错误:</p> <pre class="brush:php;toolbar:false;">&quot;code&quot;: 400, &quot;message&quot;: &quot;Invalid JSON payload received. Unknown name \&quot;to\&quot;: Cannot find field.&quot;</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' => [ &quot;to&quot; => $topic, &quot;notification&quot; => [ &quot;title&quot; => $title, &quot;body&quot; => $body, ], &quot;data&quot; => [ &quot;data&quot; => json_encode($data) ], &quot;android&quot; => [ &quot;notification&quot; => [ &quot;sound&quot; => &quot;default&quot;, &quot;title&quot; => $title, &quot;body&quot; => $body, 'tag' => $topic, &quot;channel_id&quot; => &quot;500&quot;, ], &quot;priority&quot; => &quot;high&quot;, &quot;ttl&quot; => &quot;86400s&quot; //&quot;badge&quot; => 1 ], &quot;apns&quot; => [ &quot;payload&quot; => [ &quot;aps&quot; => [ &quot;sound&quot; => &quot;default&quot; ] ], &quot;headers&quot; => [ &quot;apns-priority&quot; => &quot;5&quot;, &quot;content_available&quot; => &quot;1&quot; ], ], &quot;webpush&quot;=>[ &quot;headers&quot;=>[ &quot;Urgency&quot;=> &quot;high&quot;, //&quot;image&quot; => &quot;https://wooloveapp.com/img/misc/logo-02.jpg&quot; ] ], ] ]; $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>
P粉127901279P粉127901279439 天前534

全部回复(1)我来回复

  • P粉304704653

    P粉3047046532023-08-30 10:26:37

    您必须使用topic 而不是 to 如果您要发送到主题。

    回复
    0
  • 取消回复