Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target class [firebase.messaging] does not exist. #36

Closed
enefeka opened this issue May 10, 2020 · 13 comments
Closed

Target class [firebase.messaging] does not exist. #36

enefeka opened this issue May 10, 2020 · 13 comments

Comments

@enefeka
Copy link

enefeka commented May 10, 2020

Hi, I'm trying to send a push notification to a device but cannot seem to make it work, added this package today and followed the docs but I must be missing something. Little help would be appreciated. This is what I have so far:
Hi, I'm trying to send a push notification to a device but cannot seem to make it work, added this package today and followed the docs but I must be missing something. Little help would be appreciated. This is what I have so far:

<?php

namespace App\Services;

use Exception;
use Kreait\Firebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Database;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Exception\Auth\EmailExists as FirebaseEmailExists;

class FirebaseService
{
    /**
     * @var Firebase
     */
    protected $firebase;

    public function __construct()
    {
        $serviceAccount = ServiceAccount::fromArray([
            "type" => "service_account",
            "project_id" => config('services.firebase.project_id'),
            "private_key_id" => config('services.firebase.private_key_id'),
            "private_key" => config('services.firebase.private_key'),
            "client_email" => config('services.firebase.client_email'),
            "client_id" => config('services.firebase.client_id'),
            "auth_uri" => "https://accounts.google.com/o/oauth2/auth",
            "token_uri" => "https://oauth2.googleapis.com/token",
            "auth_provider_x509_cert_url" => "https://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url" => config('services.firebase.client_x509_cert_url')
        ]);

        $this->firebase = (new Factory)
            ->withServiceAccount($serviceAccount)
            ->create(); 
    }
}
<?php

namespace App\Providers;

use App\Services\FirebaseService;
use Illuminate\Support\ServiceProvider;
use App\Observers\UserFavouriteObserver;

class FirebaseServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('FirebaseService', function () {
            return new FirebaseService();
        });
    }
}   
<?php

namespace App\Helpers;

use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging;

class PushNotifications
{
	public function send_push()
	{
		$deviceToken = 'token';

		$notification = Notification::fromArray([
		    'title' => 'title test',
		    'body' => 'body test',
		]);

		$message = CloudMessage::withTarget('token', $deviceToken)
		    ->withNotification($notification);

		$messaging = app('firebase.messaging');

		$messaging->send($message);			
	}
}

@jeromegamez
Copy link
Member

You don’t need to instantiate the factory or anything, app('firebase.messaging') is enough to retrieve the fully configured messaging component.

You showed code, but what‘s the problem? 😅

@enefeka
Copy link
Author

enefeka commented May 10, 2020

Hey, thanks for the quick answer! Problem is I get this error:

Target class [firebase.messaging] does not exist.

@jeromegamez
Copy link
Member

Ah, missed the title 😅

Hm, strange. Have you set the FIREBASE_CREDENTIALS variable in your .env file? And could you please share the output of composer show | grep kreait?

@jeromegamez
Copy link
Member

If you prefer to use an array instead of a credentials file, you could publish the package configuration and use the array as a value for the credentials.file key (the line where it reads the environment variable by default)

@enefeka
Copy link
Author

enefeka commented May 10, 2020

Yep I do have the credentials set. This is the output of compose show | grep kreait

kreait/clock 1.0.1 A PHP 7.0 compatible cloc...
kreait/firebase-php 5.2.0 Firebase Admin SDK
kreait/firebase-tokens 1.10.0 A library to work with Fi...

@jeromegamez
Copy link
Member

jeromegamez commented May 10, 2020

From what I can see, this does indeed look alright dependency-wise 🤔

If Laravel can‘t resolve the firebase.messaging Service (it tries to interpret it as a class name, not as the service name), perhaps the ServiceProvider of the package has not been auto-discovered...

If you do a composer dump-autoload, you should see a Discovered package: kreait/laravel-firebase line. If you don‘t, are you on... wait a minute! According to your composer show output, you haven‘t installed the package 😅

(I‘m writing this all on the phone, that‘s why you are receiving my thoughts and findings unfiltered 🙈)

@enefeka
Copy link
Author

enefeka commented May 10, 2020

Thanks man, I am as stupid as it gets. I could swear I installed it, like I can see myself having done it. Must've done it for another project, or confinement has finally destroyed my brain.

Anyways, thanks again for your help, is working now!

@jeromegamez
Copy link
Member

I'm glad we figured it out together 🌺🥳

@juanzeta33
Copy link

Hi, I was having this same error, I read all your comment but I thing I have all the setup config good.

But I have continue getting the error: Class firebase.messaging does not exist

I'm using lumen with the package Kreit.

@jeromegamez
Copy link
Member

@juanzeta33 Did you add

$app->register(Kreait\Laravel\Firebase\ServiceProvider::class);

to your bootstrap/app.php file?

@juanzeta33
Copy link

juanzeta33 commented May 22, 2020

Hi, thanks for the quick answer.

Yep, I have done that.

I follow all the step (I think so) describe in the official documentation.

  1. Add the json file and the FIREBASE_CREDENTIALS in the env
  2. Add the 4 package that install from Kreait (clock, firebase-php, firebase-tokens and laravel-firebase)
  3. Call the Facades in the page that I'm trying to use the messaging.
  4. Call the $messaging = app('firebase.messaging'); for send the push notifications
  5. add the $app->register(Kreait\Laravel\Firebase\ServiceProvider::class); in the app.php

But continue having the error :(

@juanzeta33
Copy link

Hi,

Forget everything, I just forget to updated the app.php file in my dev server.

Sorry.

@kdbagwe
Copy link

kdbagwe commented Jun 15, 2024

What worked for me is updating packages.php & services.php in bootstrap/cache folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants