使用curl和api v1向主题发送firebase通知时遇到错误
P粉127901279
P粉127901279 2023-08-29 17:36:37
[PHP讨论组]

在我的 laravel 应用程序中,我使用curl 向订阅某个主题的所有用户发送通知,但是当我发送通知时,我收到此错误:

"code": 400,
"message": "Invalid JSON payload received. Unknown name \"to\": Cannot find field."

这是我使用curl发送主题notif的方式:

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;
    }

P粉127901279
P粉127901279

全部回复(1)
P粉304704653

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

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

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