使用curl和api v1向主题发送firebase通知时遇到错误
P粉127901279
P粉127901279 2023-08-29 17:36:37
[PHP讨论组]
<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' =&gt; [ &quot;to&quot; =&gt; $topic, &quot;notification&quot; =&gt; [ &quot;title&quot; =&gt; $title, &quot;body&quot; =&gt; $body, ], &quot;data&quot; =&gt; [ &quot;data&quot; =&gt; json_encode($data) ], &quot;android&quot; =&gt; [ &quot;notification&quot; =&gt; [ &quot;sound&quot; =&gt; &quot;default&quot;, &quot;title&quot; =&gt; $title, &quot;body&quot; =&gt; $body, 'tag' =&gt; $topic, &quot;channel_id&quot; =&gt; &quot;500&quot;, ], &quot;priority&quot; =&gt; &quot;high&quot;, &quot;ttl&quot; =&gt; &quot;86400s&quot; //&quot;badge&quot; =&gt; 1 ], &quot;apns&quot; =&gt; [ &quot;payload&quot; =&gt; [ &quot;aps&quot; =&gt; [ &quot;sound&quot; =&gt; &quot;default&quot; ] ], &quot;headers&quot; =&gt; [ &quot;apns-priority&quot; =&gt; &quot;5&quot;, &quot;content_available&quot; =&gt; &quot;1&quot; ], ], &quot;webpush&quot;=&gt;[ &quot;headers&quot;=&gt;[ &quot;Urgency&quot;=&gt; &quot;high&quot;, //&quot;image&quot; =&gt; &quot;https://wooloveapp.com/img/misc/logo-02.jpg&quot; ] ], ] ]; $headers = [ 'Authorization: Bearer ' .$this-&gt;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' =&gt; $result, //'statusCode' =&gt; $statusCode ]; return $result; }</pre></p>
P粉127901279
P粉127901279

全部回复(1)
P粉304704653

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号