This post is how to send web push notifications from the Laravel 8 application using the OneSignal messaging service.
This is oneSignal is a popular messaging service that allows sending push notifications, summarising details regarding the device’s platform.
This example is will give you proper instructions to integrate one signal web push notification in laravel.
Now you have to create an account at OneSignal using your email id or social media account and obtain the key and the secret id of OneSignal.
So let's start following example.
Step 1 - Download Fresh Laravel Application
composer create-project --prefer-dist laravel/laravel blogPushNotification
Step 2 - Create Database Connection
Step 3 - Update OneSignal Auth Keys
One more, again open the .env configuration file and update OneSignal auth keys to connect laravel to OneSignal.
Step 4 - Add OneSignal Package in Laravel
OneSignal in Laravel requires package installation in the Laravel app.
composer require ladumor/one-signal
Step 5 - Set Up OneSignal in Laravel
You have just installed the OneSignal package into the laravel app.
Furthermore, type the given command on the terminal and, without giving a thought, run the suggested command to publish separately create the config file.
php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"
you have to add providers and facade inside the file.
'providers' => [
'aliases' => [
'OneSignal' => \Ladumor\OneSignal\OneSignal::class,
Step 6 - Send Push Notifications
In a controller, you have to first import or use the OneSignal service from the Ladumor package. Inside the controller’s function, define $fields variable. You have to pass the player id in it.
The notification message var will hold the dynamic notification message; however, we passed it statistically.
Access sendPush() method via OneSignal module and in this message pass fields and notification message.
$fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyy']
$notificationMsg = 'Hello!! A tiny web push notification.!'
To retrieve all notifications, you can use the getNotifications method by calling,
Step 7 - Run Laravel Application
php artisan serve
I hope it can help you....