DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

Laravel 10 OneSignal Web Push Notification

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.

Step 1: Set Up a Laravel Project

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
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure .env file

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3: Install OneSignal Package

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
Enter fullscreen mode Exit fullscreen mode

Publish the config file

Run the following command to publish the config file.

php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"
Enter fullscreen mode Exit fullscreen mode

Step 4: Setup OneSignal with Laravel App

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,
Enter fullscreen mode Exit fullscreen mode

Add Facade:

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

'OneSignal' => \Ladumor\OneSignal\OneSignal::class,
Enter fullscreen mode Exit fullscreen mode

Step 5: Send Push Notification

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);
Enter fullscreen mode Exit fullscreen mode

Step 6: Run the Laravel Application

Now, run the following command in your terminal to initiate the development server:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Top comments (0)