【问题标题】:GuzzleHttp firebase messaging INVALID_ARGUMENT The message is not setGuzzleHttp firebase 消息 INVALID_ARGUMENT 消息未设置
【发布时间】:2022-07-24 16:49:22
【问题描述】:

我正在尝试实现一个使用 FirebaseCloudMessaging 发送通知的功能,我得到了 Auth 部分,但似乎 GuzzleHttp\Client 以及它如何通过 data 部分存在问题

这是我的代码

public function send(){
        putenv('GOOGLE_APPLICATION_CREDENTIALS=<path-to-json-file>.json');
        $scopes = ['https://www.googleapis.com/auth/firebase.messaging'];

        $middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
        $stack = HandlerStack::create();
        $stack->push($middleware);

        // create the HTTP client
        $client = new Client([
            'handler' => $stack,
            'base_uri' => 'https://fcm.googleapis.com',
            'auth' => 'google_auth'
        ]);

        $data = json_encode(
            [
                "message" => [
                    "topic" => "availableProviders",
                    "notification" => [
                        "title" => "New Order",
                        "body" => "New Order to accept",
                    ],
                    "data" => [
                        "service_id" => '1'
                    ],
                ],
                "validateOnly" => false,
            ],
        );

        $response = $client->post('/v1/projects/<Project-id>/messages:send', [
            [
                'json' => $data,
                'headers' => [
                    'accept' => 'application/json',
                    'Content-Type' => 'application/json',
                ],
            ]
        ]);
        dd($response);
    }

我不断收到响应 400,INVALID_ARGUMENT “消息未设置。”

Client error: POST https://fcm.googleapis.com/v1/projects//messages:sendresulted in a400 错误请求 response: { "error": { "code": 400, "message": "The message is not set.", "status": "INVALID_ARGUMENT", "details (truncated...)

虽然我的数据结构作为documented 传递,但我在这里缺少什么? 注意:我使用 Laravel,我隐藏了 等。

【问题讨论】:

    标签: php firebase-cloud-messaging


    【解决方案1】:

    尝试使用 Laravel 包装器

    use Google\Auth\ApplicationDefaultCredentials;
    use GuzzleHttp\Client;
    use GuzzleHttp\HandlerStack;
    use Illuminate\Support\Facades\Http;
    
        public function send(){
            putenv('GOOGLE_APPLICATION_CREDENTIALS=D:\Projects\waterko_final\waterco-356810-firebase-adminsdk-brisz-11133a2abb.json');
            $scopes = ['https://www.googleapis.com/auth/firebase.messaging'];
    
            $data = [
                "message" => [
                    "topic" => "availableProviders",
                    "notification" => [
                        "title" => "New Order",
                        "body" => "New Order to accept",
                    ],
                    "data" => [
                        "service_id" => '1'
                    ],
                ],
                "validateOnly" => false,
            ];
    
            $headers = [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ];
    
            $middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
            $stack = HandlerStack::create();
            $stack->push($middleware);
    
            $client = new Client([
                'handler' => $stack,
                'base_uri' => 'https://fcm.googleapis.com',
                'auth' => 'google_auth'
            ]);
    
            return Http::withHeaders($headers)->bodyFormat("json")->setClient($client)
                ->post('https://fcm.googleapis.com/v1/projects/waterco-356810/messages:send', $data);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-18
      • 2018-05-15
      • 1970-01-01
      • 2020-01-21
      • 2018-01-03
      • 1970-01-01
      • 2018-04-24
      • 2016-09-17
      相关资源
      最近更新 更多