laravel-notification-channels / fcm

Firebase Cloud Messaging (FCM) notifications channel for Laravel
https://laravel-notification-channels.com/
MIT License
495 stars 127 forks source link

My brain is not braining reading docs for multiple app or usingClient #190

Closed masterbater closed 8 months ago

masterbater commented 8 months ago

With the underlying Firebase we can set another project using config/firebase.php just add another entry like another-app

use Kreait\Laravel\Firebase\Facades\Firebase;

// Return an instance of the Auth component for the default Firebase project
$defaultAuth = Firebase::auth();
// Return an instance of the Auth component for a specific Firebase project
$appAuth = Firebase::project('app')->auth();
$anotherAppAuth = Firebase::project('another-app')->auth();

In this project, how can call underlying project like another-app, is the usingClient is the use case for this, but I dont understand this line $client = app(\Kreait\Firebase\Contract\Messaging::class);

public function toFcm(mixed $notifiable): FcmMessage
{
    $client = app(\Kreait\Firebase\Contract\Messaging::class);

    return FcmMessage::create()->usingClient($client);
}
dwightwatson commented 8 months ago

The usingClient method takes an instance of Kreait\Firebase\Contract\Messaging::class. All that example is showing you is getting an instance of that class from Laravel's container. You can instead get your own instance and pass it into the message as you like. Hope this helps.

masterbater commented 8 months ago

The usingClient method takes an instance of Kreait\Firebase\Contract\Messaging::class. All that example is showing you is getting an instance of that class from Laravel's container. You can instead get your own instance and pass it into the message as you like. Hope this helps.

My goals is to use a different project config on the fly. Please give an example of code.

dwightwatson commented 8 months ago

Inside your toFcm method create a new Firebase instance for the project you wish, and then pass the messaging component into the client method. You'll need to read up on the Kreait docs to see how to configure this as you need.

ashwin-nath-m commented 6 months ago

@masterbater can u share code for this?

JasonTolliver commented 2 months ago

In case this is helpful to anybody, this is how I pulled this off:

public function toFcm(mixed $notifiable): FcmMessage
{
    $projectManager = app(\Kreait\Laravel\Firebase\FirebaseProjectManager::class);

    $client = $projectManager->project('another-app')->messaging()

    return FcmMessage::create()->usingClient($client);
}