Laravel OneSignal Web Push Notification Tutorial

Laravel 8 one signal web push notification example; In this tutorial, i will provide you complete guide on how to integrate oneSignal in laravel 8 app for sending push notification.


OneSignal
 is a service that enables push notifications, abstracting details such as the platform the device is running on. With the OneSignal plugin, OutSystems applications can send and receive push notifications. The image below shows a push notification in an Android smartphone

Please visit the https://onesignal.com/ web site then create an account here and get secret id and key for sending web push notifications.

How to Integrate and Use One Signal in Laravel 8

  • 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

Step 1 – Install Laravel 8 App

Execute the following command on terminal to install or download Laravel 8 app:

composer create-project --prefer-dist laravel/laravel Demo

Step 2 – Connecting App to Database

Open .env and configure database details for connecting app to database:

 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:

ONE_SIGNAL_APP_ID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
ONE_SIGNAL_AUTHORIZE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X
ONE_SIGNAL_AUTH_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Step 3 – Install OneSignal Package

Execute the following command terminal to install onesignal package for send web push notification in laravel 8 app:

composer require ladumor/one-signal

Publish the config file
Run the following command to publish config file,

php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"

Step 4 – Configure OneSignal Package

After successfully install a onesignal web push notification package in the laravel app. Next, open config/app.php file and add service provider and aliases.

Add Provider
Add the provider to your config/app.php into provider section if using lower version of Laravel,

Ladumor\OneSignal\OneSignalServiceProvider::class,

Add Facade
Add the Facade to your config/app.php into aliases section,

'OneSignal' => \Ladumor\OneSignal\OneSignal::class,

Step 5 – Send Push Notification

Use the following code to send push notification in laravel apps using oneSignal web push notification:

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, execute the following command on terminal to start development server:

php artisan serve

Conclusion

Laravel 8 one signal web push notification example. In this tutorial, you have learned how to use oneSignal in laravel 8 app for sending push notification.

Recommended Laravel Posts

Be First to Comment

Leave a Reply

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