Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 8 Pusher Notification Example Tutorial

Laravel 8 Pusher Notification Example Tutorial

Posted on December 10, 2020December 15, 2022 By XpertPhp

In this tutorial, we will explain to you how to create a push notification example in Laravel 8(Laravel 8 Pusher Notification Example Tutorial). Pusher is a well-known service that allows you to send real time notifications to your applications.

In this example, we will integrate the pusher notification in laravel 8 using the “pusher-php-server” package. We will also send web push notification in laravel. so you can follow the below step.

Overview
Step 1: Install Laravel
Step 2: Install Package
Step 3: Setup your Pusher Account
Step 4: Create an event
Step 5: Create a Controller
Step 6: Define route in laravel
Step 7: Create view file in laravel
Step 8: Run The Application

Step 1: Install Laravel
We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

1
composer create-project --prefer-dist laravel/laravel laravel8_realtime_notification

Step 2: Install Package
Now, we are going to install the “pusher/pusher-php-server” package using the below command.

1
composer require pusher/pusher-php-server

Step 3: Setup your Pusher Account
First, we Must have an account on Pusher.com. if no then please go to the pusher official site and create an account.
After successfully created an account and get the appid, key, secret and cluster. we will set the credentials in the .env file. so you can see our code.

1
2
3
4
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=ap2

Step 4: Create an event
we need to create the type of event for which it works as a notification send in Laravel. Events can be triggered from anywhere in the application. the file will be created in app/Events directory. so you can create the event class using the below command.

1
php artisan make:event Notify

app/Events/Notify.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
 
namespace App\Events;
 
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
 
class Notify
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
 
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
 
    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

Step 5: Create a Controller
Here below command help to create the controller.

See also  Infinite scroll pagination using php jquery and ajax
1
php artisan make:controller PusherNotificationController

PusherNotificationController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Pusher\Pusher;
class PusherNotificationController extends Controller
{
    public function notification()
    {
        $options = array(
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true
);
        $pusher = new Pusher(
env('PUSHER_APP_KEY'),
env('PUSHER_APP_SECRET'),
env('PUSHER_APP_ID'),
$options
);
 
        $data['message'] = 'Hello XpertPhp';
        $pusher->trigger('notify-channel', 'App\\Events\\Notify', $data);
 
    }
}

Step 6: Define route in laravel
Add the following route code in the “routes/web.php” file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PusherNotificationController;
 
/*
|--------------------------------------------------------------------------
| 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('/notification', function () {
    return view('notification');
});
 
Route::get('send',[PusherNotificationController::class, 'notification']);
?>

Step 7: Create view file in laravel
So finally, first we will create the student.blade.php files in the “resources/views” directory.
notification.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<head>
  <title>Laravel 8 Pusher Notification Example Tutorial - XpertPhp</title>
  <h1>Laravel 8 Pusher Notification Example Tutorial</h1>
  <script src="https://js.pusher.com/4.1/pusher.min.js"></script>
  <script>
 
   var pusher = new Pusher('{{env("MIX_PUSHER_APP_KEY")}}', {
      cluster: '{{env("PUSHER_APP_CLUSTER")}}',
      encrypted: true
    });
 
    var channel = pusher.subscribe('notify-channel');
    channel.bind('App\\Events\\Notify', function(data) {
      alert(data.message);
    });
  </script>
</head>

Step 8: Run The Application
We can start the server and run this application using the below command.

1
php artisan serve

Now, we will use the below URL to send a notification. so you can open a new tab and visit the “http://localhost:8000/send” notification.

1
http://127.0.0.1:8000/send

if you want to see the notification then you can see the below URL and open it in another tab and you can see the alert notification.

1
http://127.0.0.1:8000/notification

Laravel, Jquery Tags:Laravel Pusher Desktop Notifications, Laravel Real Time Notification, Notification Controller

Post navigation

Previous Post: Angular 11 CRUD Application Example Tutorial
Next Post: Laravel 8 Livewire DataTable Example Tutorial

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com