Firebase Push Notification to Android and IOS App Example in laravel

Hi All My Laratuto viewer,

In this tutorial, you will learn laravel-8 push notification to android and ios with firebase. We will look at example of laravel-8 firebase push notification android. i would like to show you laravel-8 push notification android. if you want to see example of laravel-8 push notifications ios android then you are a right place.

Today, i will give you simple example of send firebase push notification to android and ios app a using laravel. you can also use in laravel 6, laravel 7 and laravel 8 version.

Here, i will give you very simple step to follow me sending push notification to android and ios app using laravel-8 application. so basically your front-end mobile application provides you device token and you will store on database. Then you will use that device token to sending push notification using firebase laravel-8 . so let’s follow bellow steps:

passport install in laravel

Preview:

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 web app on that project as like i added bellow screenshot and follow this:

After created successfully created app we will go to setting page and get server api key as like bellow screen shot: Click To Next…………

Step 2: Create Route:

Here, we need to add some routes to send push notification to android and ios so let’s add that route in web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NotificationSendController;
  

   
Route::get('push-notification', [NotificationSendController::class, 'index']);
Route::post('sendNotification', [NotificationSendController::class, 'sendNotification'])->name('send.notification');

Step 3: Create Controller.

app/Http/Controllers/NotificationSendController.php

<?php
 
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\MdlUser\User;
  
class NotificationSendController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        return view('pushSendNotification');
    } 
  
     /**
     * Write code on Method
     *
     * @return response()
     */
    public function sendNotification(Request $request)
    {
        $firebaseToken = User::whereNotNull('device_token')->pluck('device_token')->all();
            
        $SERVER_API_KEY ="$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);
        curl_close($ch);
    
        return back()->with('success', 'Push Notification send successfully.');
    }
}

Step 4: Update Blade File

In this step, we will create pushNotification.blade.php file with following below code copy and paste this code:

resources/views/pushSendNotification.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Laravel Firebase Push Notification to Android and IOS App Example - Laratuto.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  
<br/>
<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') }}
                        </div>
                    @endif
    
                    <form action="{{ route('send.notification') }}" method="POST">
                        @csrf
                        <div class="form-group">
                            <label>Title</label>
                            <input type="text" class="form-control" name="title">
                        </div>
                        <div class="form-group">
                            <label>Body</label>
                            <textarea class="form-control" name="body"></textarea>
                          </div>
                        <button type="submit" class="btn btn-primary">Send Notification</button>
                    </form>
    
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Run Laravel App:

php artisan serve

Leave a Comment