- Notifications
You must be signed in to change notification settings - Fork132
Firebase Cloud Messaging (FCM) notifications channel for Laravel
License
laravel-notification-channels/fcm
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This package makes it easy to send notifications usingFirebase Cloud Messaging (FCM) with Laravel.
Install this package with Composer:
composer require laravel-notification-channels/fcm
This package now uses thelaravel-firebase library to authenticate andmake the API calls to Firebase. Follow theconfigurationsteps specified in their readme before using this.
After following their configuration steps, make sure that you've specified yourFIREBASE_CREDENTIALS
in your .envfile.
After setting up your Firebase credentials, you can now send notifications via FCM by a Notification class and sendingit via theFcmChannel::class
. Here is an example:
useIlluminate\Notifications\Notification;useNotificationChannels\Fcm\FcmChannel;useNotificationChannels\Fcm\FcmMessage;useNotificationChannels\Fcm\Resources\NotificationasFcmNotification;class AccountActivatedextends Notification{publicfunctionvia($notifiable) {return [FcmChannel::class]; }publicfunctiontoFcm($notifiable):FcmMessage {return (newFcmMessage(notification:newFcmNotification( title:'Account Activated', body:'Your account has been activated.', image:'http://example.com/url-to-image-here.png' ))) ->data(['data1' =>'value','data2' =>'value2']) ->custom(['android' => ['notification' => ['color' =>'#0A0A0A','sound' =>'default', ],'fcm_options' => ['analytics_label' =>'analytics', ], ],'apns' => ['payload' => ['aps' => ['sound' =>'default' ], ],'fcm_options' => ['analytics_label' =>'analytics', ], ], ]); }}
You will have to set arouteNotificationForFcm()
method in your notifiable model.This method should return the user's FCM token(s) from storage.For example:
class Userextends Authenticatable{use Notifiable;.../** * Specifies the user's FCM token * * @return string|array */ publicfunction routeNotificationForFcm() {return$this->fcm_token; }}
You can also return an array of tokens to send notifications via multicast to different user devices.
class Userextends Authenticatable{use Notifiable;.../** * Specifies the user's FCM tokens * * @return string|array */ publicfunction routeNotificationForFcm() {return$this->getDeviceTokens(); }}
Once you have that in place, you can simply send a notification to the user by doing the following:
$user->notify(newAccountActivated);
View theFcmMessage
source for the complete list of options.
FcmMessage::create() ->name('name') ->token('token') ->topic('topic') ->condition('condition') ->data(['a' =>'b']) ->custom(['notification' => []]);
You can change the underlying Firebase Messaging client on the fly if required. Provide an instance ofKreait\Firebase\Contract\Messaging
to your FCM message and it will be used in place of the default client.
publicfunctiontoFcm(mixed$notifiable):FcmMessage{$client =app(\Kreait\Firebase\Contract\Messaging::class);return FcmMessage::create()->usingClient($client);}
When a notification fails it will dispatch anIlluminate\Notifications\Events\NotificationFailed
event. You can listen for this event and choose to handle these notifications as appropriate. For example, you may choose to delete expired notification tokens from your database.
<?phpnamespaceApp\Listeners;useIlluminate\Notifications\Events\NotificationFailed;useIlluminate\Support\Arr;class DeleteExpiredNotificationTokens{/** * Handle the event. */publicfunctionhandle(NotificationFailed$event):void {if ($event->channel == FcmChannel::class) {$report = Arr::get($event->data,'report');$target =$report->target();$event->notifiable->notificationTokens() ->where('push_token',$target->value()) ->delete(); } }}
Remember to register your event listeners in the event service provider.
/** * The event listener mappings for the application. * * @var array */protected$listen = [ \Illuminate\Notifications\Events\NotificationFailed::class => [ \App\Listeners\DeleteExpiredNotificationTokens::class, ],];
Please seeCHANGELOG for more information what has changed recently.
composertest
If you discover any security related issues, please emailchrisbjr@gmail.com instead of using the issue tracker.
Please seeCONTRIBUTING for details.
The MIT License (MIT). Please seeLicense File for more information.
About
Firebase Cloud Messaging (FCM) notifications channel for Laravel
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.