Home Laravel How To Create FCM Push Notification In Laravel 9?

How To Create FCM Push Notification In Laravel 9?

by Shahriar Sagor

Hello Dev , today now in this post i will give you an example of laravel 9 fcm push notification example. Here I would like to show you laravel 9 firebase fcm push notification example. So in this article i will give you a very simple example of fcm push notification in laravel 9. So let’s discuss about the send fcm notification in laravel 9. We will do the following things for laravel 9 send push notification to android.

Then i will use that device token to send push the notifications by using firebase laravel. So let’s follow the below steps:

Step 1: Install Laravel

At first of all we have to get the fresh Laravel version application by using the bellow command. So open your terminal OR command prompt and then run the bellow command:

composer create-project laravel/laravel example-app
Step 2: Create Firebase Project and App

Now in first step, we have to go Firebase Console and then need to create a project. then we have to create the web app on that project as like as i added bellow screenshot:

FCM Push Notification In Laravel

After the created successfully created app i will go to setting page and then get server api key as like as bellow screen shot:

FCM Push Notification In Laravel

we can copy that key and then add on env file as bellow:


Step 3: Create Route

Here, i have to add the some routes to send the push notification so let’s add that route in web.php file.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NotificationController;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
Route::get('push-notification', [NotificationController::class, 'index']);
Route::post('sendNotification', [NotificationController::class, 'sendNotification'])->name('send.notification');
Step 4: Create Controller

Here, i have to add index() and sendNotification() method for the admin route in NotificationController.

In sendNotification() method i will get all the device token from the users table and then send notification to user. Also make sure you have “device_token” column in the users table and added token there.

now, so let’s add as like as bellow:


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class NotificationController extends Controller
     * Write code on Method
     * @return response()
    public function index()
        return view('pushNotification');
     * Write code on Method
     * @return response()
    public function sendNotification(Request $request)
        $firebaseToken = User::whereNotNull('device_token')->pluck('device_token')->all();
        $SERVER_API_KEY = env('FCM_SERVER_KEY');
        $data = [
            "registration_ids" => $firebaseToken,
            "notification" => [
                "title" => $request->title,
                "body" => $request->body,  
        $dataString = json_encode($data);
        $headers = [
            'Authorization: key=' . $SERVER_API_KEY,
            'Content-Type: application/json',
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
        $response = curl_exec($ch);
        return back()->with('success', 'Notification send successfully.');
Step 5: Update Blade File

Now in this step, i will be create pushNotification.blade.php file with the following code:


<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Laravel Firebase Push Notification to Android and IOS App Example - CodingsPoint.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Dashboard') }}</div>
                <div class="card-body">
                    @if (session('success'))
                        <div class="alert alert-success" role="alert">
                            {{ session('success') }}
                    <form action="{{ route('send.notification') }}" method="POST">
                        <div class="form-group">
                            <input type="text" class="form-control" name="title">
                        <div class="form-group">
                            <textarea class="form-control" name="body"></textarea>
                        <button type="submit" class="btn btn-primary">Send Notification</button>

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook


You may also like