Welcome to the world of Laravel 10 and OneSignal web push notifications! In this guide, we'll show you how to use Laravel 10 to add web push notifications to your website. These notifications are a great way to keep your users informed and engaged with your content.
Let's dive in and see how you can enhance your web presence with this exciting feature.
So, let's see how to set up OneSignal web push notifications in laravel 8, laravel 9, and laravel 10.
If you haven't already, create a new Laravel project or use an existing one.
composer create-project --prefer-dist laravel/laravel onesignal-push-notification
Now, we will configure the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
DB_PASSWORD=password
After that, we will configure the Onesignal APP ID and Key.
ONE_SIGNAL_APP_ID=XXXX
ONE_SIGNAL_AUTHORIZE=XXXX
ONE_SIGNAL_AUTH_KEY=XXXX
Open your terminal and navigate to your Laravel project directory. Install the required OneSignal package to send web push notifications.
composer require ladumor/one-signal
Publish the config file
Run the following command to publish the config file.
php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"
Once you've installed a OneSignal web push notification package in your Laravel app, the next step is to configure your config/app.php
file by adding the required service providers and aliases.
Add Provider:
Include the provider in the providers
section of your config/app.php
. This is necessary, especially if you are using a lower version of Laravel.
Ladumor\OneSignal\OneSignalServiceProvider::class,
Add Facade:
Add the Facade to your config/app.php into the aliases section.
'OneSignal' => \Ladumor\OneSignal\OneSignal::class,
In this step, you'll need to check out this code to initiate the process of sending a push notification in your Laravel app using the OneSignal web push notification feature.
use Ladumor\OneSignal\OneSignal;
$fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyy']
$message = 'push notification example'
OneSignal::sendPush($fields, $message);
Now, run the following command in your terminal to initiate the development server:
php artisan serve
You might also like: