laravel-notification-channels / fcm

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

Is it possible to send On-Demand Notification to AnonymousNotifiable? #181

Closed jamesbhatta closed 7 months ago

jamesbhatta commented 7 months ago

I am trying to send notification to a specific device of user. In my case a user have multiple devices FCM tokens. I guess On-Demand Notification was what I need in this situation. But its not working with package. Did I missed something?

Triggering as:

 Notification::route(FcmChannel::class, 'FCM_TOKEN')
    ->notify(new \App\Notifications\TestNotification());

I was able to make it work by making the following change in send method of FcmChannel::class

if ($notifiable instanceof \Illuminate\Notifications\AnonymousNotifiable) {
   $tokens = Arr::wrap($notifiable->routes[FcmChannel::class]);
} else {
   $tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification));
}
dwightwatson commented 7 months ago

I think you just have to use fcm in place of the channel when routing.

 Notification::route('fcm', 'FCM_TOKEN')
    ->notify(new \App\Notifications\TestNotification());

I'm not sure if there is a better convention for how to do this with non-first party notification channels, but seeing as we call routeNotificationFor('fcm') it should be wired up to work for this.

jamesbhatta commented 7 months ago

Damm, so stupid of me. Thank you, its working absolutely fine. Would be so great to see this in docs.