combi35
combi35

Reputation: 497

Laravel kreait package unable to create the messaging service without a project ID

I would like to use Firebase cloud messaging, so I installed kreait laravel firebase package I created a service account in firebase console, included the downloaded .json file in my root folder in laravel and used

FIREBASE_CREDENTIALS=app-XXXXX-firebase-adminsdk-XXXX-XXXXXXX.json

in .env


Also created a controller for sending a push notification to a device

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;

class PushNotifactionController extends Controller
{
    public function sendPush(){
        $messaging = app('firebase.messaging');
        $deviceTokens = Auth::user()->deviceTokens()->pluck('device_token')->toArray();

        $message = CloudMessage::new();
        $message->withNotification(Notification::create('Title', 'Body'));

        $sendReport = $messaging->sendMulticast($message, $deviceTokens);
        
        return response()->json([
            'Successful' => $sendReport->successes()->count(),
            'Failed' => $sendReport->failures()->count()
        ]);
    }
}

If I launch my Flutter app, firebase package creates a device token which i store in my database. But if I would like to trigger the sendPush() with postman i got this error:

"message": "Unable to create the messaging service without a project ID",
"exception": "Kreait\\Firebase\\Exception\\RuntimeException",

From the firebase console I was able to send a notification to my phone.

The package should autodiscover the service account and project ID. Help very appreciated!

Upvotes: 3

Views: 9858

Answers (1)

Davide Capriglione
Davide Capriglione

Reputation: 43

After some trying i find this command

php artisan optimize:clear

The problem was that in the file "Factory.php" of Kreait library the function that search for firebase .json file use command

Util::getenv('FIREBASE_CREDENTIALS')

At first I unlock this file for writing and check if the command "env('FIREBASE_CREDENTIALS')" work, and it doesn't work, after the command that i write up all work perfectly.

Upvotes: 3

Related Questions