GuzzleHttp firebase messaging INVALID_ARGUMENT The message is not set

Viewed 66

I'm trying to implement a function to send a a notification using FirebaseCloudMessaging, I got the Auth part, But seems there is an issue with the GuzzleHttp\Client and how it passes the data part

This is my code

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

I keep getting that response 400, INVALID_ARGUMENT "The message is not set."

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

Although my data structure is being passed as documented, what I'm missing here? Note: I use Laravel, I hided the , etc.

1 Answers

Try to use the Laravel wrapper

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