kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
994 stars 163 forks source link

Call to undefined method Illuminate\Notifications\Channels\MailChannel::create() #78

Closed robertnicjoo closed 3 years ago

robertnicjoo commented 3 years ago

I am getting this error when trying to send FCM

Call to undefined method Illuminate\Notifications\Channels\MailChannel::create()
Route::get('/fcm', function() {
    $factory = Firebase::auth();
    $topic = 'From Web';
    $messaging =  app('firebase.messaging');
    $message = CloudMessage::withTarget('topic', $topic)
            ->withNotification(Notification::create('testing', 'working ok'))
            ->withData(['test' => 'value']);

    $m = $messaging->send($message);

    echo $m;
});

The error coming from (Notification::create('testing', 'working ok') =>Notification

jeromegamez commented 3 years ago

Have you imported all the required classes with use statements?

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

Especially the last one is important, in your case, it seems that you're actually using Illuminate\Support\Facades\Notification which is causing the error you got.

Side note: If you're using Firebase::auth() to retrieve the Auth component, you can use Firebase::messaging() in the same fashion.

robertnicjoo commented 3 years ago

@jeromegamez Thank you notification error is gone now, but I don't receive any notification not in my firebase panel nor on my phone, all I get is this on my browse as result of echo $m

{"name":"projects\/test1-295256\/messages\/6349794722946404889"}

Update

What is withTarget()? can I not use that? I want all my users get those notifications (target is everyone 😄 )

jeromegamez commented 3 years ago

Messages sent from an Admin SDK/via the Firebase REST APIs don't show up in the Firebase console. But if you don't get an error when sending a message, this means that Firebase has accepted the message, and it's out of "our hands" now. All we know is that the message was valid, that the target of the message (in this case, an FCM Topic) is valid and registered to your Firebase project.

The delivery to your phone is determined by the speed that Firebase delivers the message and that your phone is actually subscribed to the topic.

You can check the topics your phone is subscribed to with the App Instance API. With the registration token of your device, you can get its currently subscribed topics as described at https://firebase-php.readthedocs.io/en/5.14.1/cloud-messaging.html#app-instance-management

jeromegamez commented 3 years ago

What is withTarget()? can I not use that? I want all my users get those notifications (target is everyone 😄 )

There's no "automatic" way to send a message to everyone - if you want to send a message to multiple users, you can use a topic (as you did in your example), but for this to work, everyone must be subscribed to the topic first 😅

You've certainly already seen https://firebase-php.readthedocs.io/en/5.14.1/cloud-messaging.html , you can find information on how to subscribe clients to topics here: