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": {} } }
Advertisement
Answer
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 IlluminateHttpClientResponse
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 IlluminateHttpClientResponse
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.