How to Send OneSignal Web Push Notification in Laravel 9

Here we are going to discuss on aravel 9 OneSignal send push notification. This guide will make you learn how to send push notifications from a Laravel app using the OneSignal messaging service.

OneSignal is a popular messaging service which allows you to send push notifications, and summarizes all details in the the device’s platform.

In this example, we will learn how to configure the OneSignal plugin in your Laravel application to send and receive web push notifications.

We will provide you with proper instructions to integrate one signal web push notification in Laravel. However, you need to create an account at OneSignal using your email id or social media account and get a key and the secret id from it.

Use One Signal to Send Web Push Notification – 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

Firstly, make sure you have created a basic Laravel application. Otherwise, execute the command given below from your terminal screen.

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

Next, get inside your new app.

cd my-demo-app

Make Database Connection

Secondly, you need to open your .env config file and add your database name, username and password to make a connection between Laravel and your database.

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

In case you are using the mac operating system and MAMP local server, ensure to add UNIX_SOCKET and DB_SOCKET database credentials in your .env file.

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

Update OneSignal Auth Keys

Next, open your .env configuration file and update OneSignal authentication keys to connect your Laravel app 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 to send web push notifications to users. Moreover, you can also rely on its accuracy and power.

To implement OneSignal in your Laravel application, you need the package installation in your Laravel app.

composer require ladumor/one-signal

Set Up OneSignal in Laravel

We have installed the OneSignal package into our Laravel app.

Next, type the command given below to your terminal and run it to publish and create a config file.

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

Now, in the very next step, you need to add providers and facade in your config/app.php file.

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

Send Push Notifications

In your controller, you need to first import the OneSignal service from Ladumor package. Inside the controller’s function, define $fields variable. You need to pass the player id to it.

The notification message variable will hold the dynamic notification message. However, we have already passed it statistically.

Call the 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);

Use the getNotifications method, to retrieve all fields. You may call it as shown below.

OneSignal::getNotifications();

Run Laravel Application

Finally, you are now ready to start your Laravel application and type the recommended command to invoke your development server.

php artisan serve

Summary

This tutorial on OneSignal use in Laravel, showed us the fundamentals to send push notifications to users from the Laravel ecosystem.

We followed step by step and comprehended the complete process to implement one signal in Laravel, and we hope this guide will help you do implement the same.