Laravel

Firebase web push notification in Laravel 8

Hello everyone in this article, we will get to know about how we can send push notifications in Laravel 8. I would like to show you laravel 8 firebase web push notification. This article goes into detail on firebase web push notification laravel 8. you’ll learn to send firebase push notifications using laravel 8.

Before moving forward Let’s know what is web push notification?

If you’ve subscribed to a brand’s web push notifications, then you’re no stranger to the messages that periodically pop up at the top right of your desktop screen. 

Web push notifications (also known as browser push notifications) are actionable messages sent to a visitor’s device from a website via a browser. These messages are contextual, timely, personalized, and best used to engage, re-engage, and retain website visitors. 

I hope now have the basic idea of a web push notification, now let’s start the integration.

Will follow below step to integrate firebase web push notification in laravel

  1. Create Firebase Project and App
  2. Install Laravel 8
  3. Create Auth in laravel
  4. Create migration and update model
  5. Create route
  6. Add code in controller
  7. Add blade
  8. Create Firebase Service Worker
  9. Register Service worker
  10. Test the integration

Step: 1 Create Firebase Project and App

In the first step, we have to go Firebase Console and create a project. then you have to create a web app for that project as I added the below screenshot:

firebase project console

After the given name and next then you will receive firebase SDK as like the bellow screenshot:

firebase token page

You have to save that all information because we will use it later in our app.

Step: 2 Install Laravel 8

first of all, we need to get a fresh Laravel 8 application using bellow command, So open your terminal OR command prompt and run bellow command:

PHP

Step: 3 Create Auth

Now, in this step, we will create auth scaffold command to create a login, register, and dashboard. so run the following commands:

Before creating the auth you have to install node module, to install node modules please add the below code.

PHP
PHP

After that use the below code to create auth scaffolding

PHP

Step:4 Create model and migration

In this step, we need to add a new column “device_token” in the user’s table and model. then we need to create a new migration. so let’s create new migration by following the command.

PHP

database/migrations/2021_10_23_144523_add_column_device_token.php

PHP

Now we also have to add device_token in our user’s model page app/Models/User.php

PHP

now run the migration command to add device_token column to your user’s table

PHP

Step:5 Create route

Now we will create route for our firebase web push notification application, add the below route in your routes/web.php file

PHP

Step:6 Add Code in controller

Now we will add the code in the HomeController, HomeController will exist inside app/http/controller now add the below code in HomeController.

PHP

Step:7 Add code in blade file

Now we will add the code in our blade file, go to resources/views/home.blade.php and add the below code into it.

HTML

Step: 8 Create firebase service worker

Before going towards adding the code, let’s know what is the service worker. A service worker is a script that your browser runs in the background, separate from a web page.

Now I think you have a basic idea that why we have to add this firebase service worker in order to receive web push notifications. Create a file named as firebase-messaging-sw.js under your resources/js folder and the below code into it.

JavaScript

Step:9 Register the service worker

Now we have to register this service worker, to register this service worker go resouces/js/ and open app.js file and the below code into it.

JavaScript

Step:10 Test the integration

Now it’s to test the integration, let’s see how our code works go to your project root folder and type php artisan serve . It will starts the development server.

Now go to your browser and enter this url in your chrome browser http://localhost:8000/ when you open this url you have something like the below image.

laravel firebase project authentication

Now here you can register yourself. once you registered yourself will have something like the below image.

firebase after registration page.

Now open the same URL in your edge for firefox browser, to receive notifications.

NOTE: Notification can’t be sent on the same browser, you need a browser on to sent and one to receive the notification.

When open http://localhost:8000/ in your edge browser login with your credential which you have used to register yourself on chrome.

Now here click on allow for notification.When you click on it you will get the confirm popup message like the below one.

Now it’s time to send a notification to go to the chrome browser, and then in the title enter codehunger and in the description add I love codehunger and click on send notification.

Now you can see in the below image, that notification has been received.

Hurray! we have successfully done the integration, I hope in your case it’s working you can also download the code directly from Github.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button