'Get empty result api firebase [FCM]

I've saved some of my mobile registration_codes that are connected with my development environment of Firebase. Once I send a manual notification by the api I receive empty feedback from Firebase himself. After debugging I found that the notification has not been send. What's wrong with my call, because the call I make is the same in the examples and the registration_code is exactly the code what I receive from Flutter libary of Firebase.

code:

$response = Http::withToken(env('FCM_TOKEN'))->post(self::GLOBAL_URL, [
    'registration_ids' => $request->users,
    'data' => [
        'title' => $request->title,
        'message' => $request->message,
    ],
]);

return response()->json(["result" => $response]);

result:

{
"result": {
    "cookies": {},
    "transferStats": {}
  }
}


Solution 1:[1]

First, you should not use env directly, because your .env file will not be used when the configuration is cached. Instead, you should set this value in your config/services.php file:

return [
  'firebase' => [
    'fcm_token' => env('FCM_TOKEN')
  ]
];

Second, your issue is that you are serializing the Illuminate\Http\Client\Response object instead of getting its value. To return the JSON result of your request, call json():

$response = Http::withToken(config('firebase.fcm_token'))->post(self::GLOBAL_URL, [
    'registration_ids' => $request->users,
    'data' => [
        'title' => $request->title,
        'message' => $request->message,
    ],
])->json(); // Notice the `json` call here


return response()->json(["result" => $response]);

If you inspect the Illuminate\Http\Client\Response object itself, you will see that it contains cookies and transferStats properties (specifically, they are on the PendingRequest object). That's why you are seeing this in your result.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Enzo Innocenzi