搜尋

首頁  >  問答  >  主體

使用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粉127901279451 天前552

全部回覆(1)我來回復

  • P粉304704653

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

    您必須使用topic 而不是 to 如果您要傳送到主題。

    回覆
    0
  • 取消回覆