Skip to content

All topics  /  Laravel

How To OneSignal Send Web Push Notification In Laravel 10?

Laravel ·

Teams applying “How To OneSignal Send Web Push Notification In Laravel 10?” in a production project can use the linked project resource to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hello Friends,

This post is about how to send web push notifications from the Laravel 10 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 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 the example.

Step 1: Download Laravel


Let us begin the tutorial by installing a new Laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

Step 2: Database Configuration

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_user_name
DB_PASSWORD=your_password

Step 3: Update OneSignal Auth Keys

One more, again open the .env configuration file and update OneSignal auth keys to connect Laravel to OneSignal.

.env

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

Step 4: Add OneSignal Package

OneSignal in Laravel requires package installation in the Laravel app.

composer require ladumor/one-signal

Step 5: SetUp OneSignal

You have just installed the OneSignal package into the Laravel app.

Furthermore, type the given command on the terminal and, without giving it a thought, run the suggested command to publish separately and create the config file.

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

you have to add providers and facade inside the file.

config/app.php

<?php
return[
    ......
    'providers' => [
        ...
        ...
        Ladumor\OneSignal\OneSignalServiceProvider::class,
    ],
    '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 the $fields variable. You have to pass the player id into it.

The notification message var will hold the dynamic notification message; however, we passed it statistically.

Access the sendPush() method via the OneSignal module and in this message pass fields and notification message.

use Ladumor\OneSignal\OneSignal;
$fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyy']
$notificationMsg = 'Hello!! A tiny web push notification.!'
OneSignal::sendPush($fields, $notificationMsg);

To retrieve all notifications, you can use the getNotifications method by calling,

OneSignal::getNotifications();

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the Laravel app:

php artisan serve

Now, you have to open the web browser, type the given URL and view the app output: