douglasresendemaciel / fcm-laravel-notification
Driver to connect laravel´s notification system to Google Firebase Cloud Messaging
Installs: 8 811
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 3
Forks: 6
Open Issues: 1
Requires
- guzzlehttp/guzzle: ^6.2
- illuminate/contracts: ^5.3
- illuminate/notifications: ^5.3
- illuminate/support: ^5.3
This package is auto-updated.
Last update: 2025-02-09 14:56:06 UTC
README
This library allow send push notifications to Firebase Cloud Messaging through laravel´s notifications.
It´s possible send notifications to websites, Android and IOS without any other integration.
Installation
Run the following command from you terminal:
composer require "douglasresendemaciel/fcm-laravel-notification:@dev"
or add this to require section in your composer.json file:
"douglasresendemaciel/fcm-laravel-notification"
then run composer update
Once it is installed, you need to register the service provider. Open up config/app.php and add the following to the providers key.
'providers' => [ ... DouglasResende\FCM\NotificationServiceProvider::class ...
Usage
First, create your notification class using artisan php artisan make:notification MyNotification
Implement your notification:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use DouglasResende\FCM\Messages\FirebaseMessage; class MyNotification extends Notification { use Queueable; public function via($notifiable) { return ['fcm']; } public function toFcm($notifiable) { return (new FirebaseMessage())->setContent('Test Notification', 'This is a Test'); } }
Implement on your notifiable
use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; class User extends Model { use Notifiable; public function routeNotificationForFcm() { //return a device token, either from the model or from some other place. return $this->device_token; } }
Open up config/broadcasting.php and add the Firebase api key to the connections section:
... 'fcm' => [ 'key' => env('FCM_API_KEY','YOUR_API_KEY') ] ...
Trigger notification:
$User = User::find(1); //Send Notification $User->notify(new MyNotification());
References
For more information read the official documentation at https://laravel.com/docs/5.3/notifications