Laravel with Firebase Cloud Messaging FCM Notification Part 2 “store users notification on the database and fetch it with queue and supervisor”
2 min readDec 25, 2020
#Note parte 1 https://medium.com/@ibrahim.s.m.2010/laravel-with-firebase-cloud-messaging-fcm-part-1-64de03134c00
Database Notifications :
Step1: Create a migration for the notifications table
php artisan notifications:table
Step2: Migrate the notifications table
php artisan migrate
Step3: Create a new notification class
php artisan make:notifiaction PushNotification
Step4: Update PostObserver to send a notification with class PushNotification
Step5: Setup Notification Events
When a notification is sent, the Illuminate\Notifications\Events\NotificationSent
event is fired by the notification system. This contains the "notifiable" entity and the notification instance itself. You may register listeners for this event in your EventServiceProvider
:
protected $listen = [
'Illuminate\Notifications\Events\NotificationSent' => [
'App\Listeners\LogNotification',
],
];
Step6: Create LogNotification and handle send notification
Step7: add icon notification and badge count in ‘layouts.app’ navbar
Step8: Setup View Composers to share notifications
Add it in the app.php/providers list
App\Providers\ViewServiceProvider::class,
Step9: now $notification and $notificationCount share with ‘layouts.app’
Step10: now it’s ready to push notifications
Step11: Supervisor Configuration
#Note change from env QUEUE_CONNECTION=database
# install supervisor
sudo apt-get install supervisorSupervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let's create a laravel-worker.conf file that starts and monitors a queue:work process:[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log
stopwaitsecs=3600
for example it local fcmapp
command=php /var/www/html/fcmapp/artisan queue:work sqs --sleep=3 --tries=3stdout_logfile=/var/www/html/fcmapp/worker.log