Laravel 8 OneSignal Send Web Push Notification

Dec 27, 2021 . Admin



Hello Dev,

Now let's see example of how to use OneSignal send web push notification example. We will check how to use OneSignal send web push notification. This is a short guide on OneSignal send web push notification in laravel 8. Let's get started with how to use OneSignal send web push notification in laravel 8.

Here i will give you many example how to use OneSignal send web push notification using laravel 8.

Step 1 : Install fresh Laravel Application

Let's run the following command.

composer create-project --prefer-dist Laravel/Laravel newBlog
Step 2 : Create Database Connection

.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.

ONE_SIGNAL_APP_ID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
ONE_SIGNAL_AUTHORIZE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ONE_SIGNAL_AUTH_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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.

Path: 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 $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.

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();
Step 7 : Run Laravel Application

Now, run example by following command:

php artisan serve

It will help you...

#Laravel 8 #Laravel