How to Send OneSignal Web Push Notification in Laravel 9

Laravel 9 OneSignal send push notification tutorial; This swift guide will show you how to send web push notifications from the Laravel application using the OneSignal messaging service.

OneSignal is a popular messaging service that allows sending push notifications, summarising details regarding the device’s platform.

In this Laravel oneSignal send notification example, we will show you how to configure the OneSignal plugin in the Laravel app to send and receive web push notifications.

We will give you proper instructions to integrate one signal web push notification in laravel; nonetheless, 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.

How to Use One Signal to Send Web Push Notification in Laravel 9

  • Step: 1 Create Laravel Project
  • Step: 2 Make Database Connection
  • Step: 3 Update OneSignal Auth Keys
  • Step: 4 Add OneSignal Package in Laravel
  • Step: 5 Set Up OneSignal in Laravel
  • Step: 6 Send Push Notifications
  • Step: 7 Run Laravel Application

Create Laravel Project

Before you start, make sure you have a basic laravel application is created.

If not, go ahead and execute the given below command from the terminal’s screen.

composer create-project laravel/laravel my-demo-app --prefer-dist

Further, step inside the app:

cd my-demo-app

Make Database Connection

In this second step, you will have to open the .env config file and add the database name, username and password to make the laravel and database connection.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=

If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

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

Add OneSignal Package in Laravel

Laravel OneSignal helps you send web push notifications to users; moreover, you can rely on its fidelity and power.

Implementing OneSignal in Laravel requires package installation in the Laravel app.

composer require ladumor/one-signal

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"

Now, in the subsequent step, you have to add providers and facade inside the config/app.php file.

<?php
return [
    'providers' => [
        ...
        ...
        Ladumor\OneSignal\OneSignalServiceProvider::class,
    ],
    'aliases' => [
        ...
        ...
        'OneSignal' => \Ladumor\OneSignal\OneSignal::class,
    ]

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();

Run Laravel Application

Ultimately, you are utterly ready to start the laravel application and type recommended command to invoke the development server.

php artisan serve

Summary

This Laravel and OneSignal tutorial showed you some fundamentals for sending push notifications to users from the laravel ecosystem.

We went step by step and comprehended the entire process to implement one signal in laravel, and we believe this guide will help you.

Leave a Comment