In this tutorial you will learn about the Laravel OneSignal Web Push Notification Example and its application with practical example.
In this Laravel OneSignal Web Push Notification Example tutorial, I’ll show you how to integrate and send web push notification using onesignal in laravel 8 application. In this tutorial you will learn integrate onesignal and send web push notification using onesignal in laravel project. In this article I will share example to integrate onesignal web push notification in laravel.
Laravel OneSignal Web Push Notification Example
In this step by step tutorial I will demonstrate you the process to integrate onesignal and to send web push notification in laravel. Please follow the instruction given below:
- Step 1 – Install Laravel 8 App
- Step 2 – Connecting App to Database
- Step 3 – Install OneSignal Package
- Step 4 – Configure OneSignal Package
- Step 5 – Send Push Notification
- Step 6 – Run Development Server
Before starting with this tutorial please visit the https://onesignal.com/ web site then create an account here and get secret id and key to send web push notifications from your laravel application.
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Laravel-QR-Code |
Step 2 – Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
And as well as, configure a .env file with following keys:
1 2 3 4 |
ONE_SIGNAL_APP_ID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX ONE_SIGNAL_AUTHORIZE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX X ONE_SIGNAL_AUTH_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
Step 3 – Install OneSignal Package
In this step, we will install OneSignal Package via the composer dependency manager. Use the following command to install OneSignal Package.
1 |
composer require ladumor/one-signal |
Now, run the following command to publish config file,
1 |
php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider" |
Step 4 – Configure OneSignal Package
After Installing Image intervention package, we need to add service provider and alias in config/app.php file as following.
Add Provider
Add the provider to your config/app.php into provider section if using lower version of Laravel,
1 |
Ladumor\OneSignal\OneSignalServiceProvider::class, |
Add Facade
Add the Facade to your config/app.php into aliases section,
1 |
'OneSignal' => \Ladumor\OneSignal\OneSignal::class, |
Step 5 – Send Push Notification
Now, below is example code to send web push notification using onesignal:
1 2 3 4 |
use Ladumor\OneSignal\OneSignal; $fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyy'] $message = 'hey!! This is a test push.!' OneSignal::sendPush($fields, $message); |
Step 6 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |