Laravel 10 OneSignal Web Push Notification Integration Tutorial

Last Updated on by in Laravel

In this tutorial, you’ll learn how to integrate OneSignal with a Laravel application to send web push notifications.

Push notifications are a powerful way to keep users engaged with your application by delivering timely, relevant messages directly to their devices.

We’ll guide you through setting up OneSignal in your Laravel project, configuring your application for push notifications, and sending notifications from within your application using the OneSignal API.

To integrate one signal web push notification in laravel, 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.

We’ll start by installing the OneSignal package and setting up your environment variables. Then, we’ll show you how to configure the package in your Laravel application, including setting up the necessary credentials and URL endpoints.

After that, you’ll learn how to create and send web push notifications using Eloquent models and the OneSignal package.

By the end of this tutorial, you’ll be able to send web push notifications from your Laravel application using OneSignal, enhancing user engagement and providing a better experience for your audience.

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

  • 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

Sending web push notifications from your Laravel application using OneSignal is straightforward.

By following this guide, you’ve mastered the setup and configuration, allowing you to keep users informed and engaged.

Age calculator tool

🎂✨ Discover your age in detail and with countdown to your next birthday with fun facts! 🎈🎉

Calculate your age now!
Digamber - Author positronX.io

Hi, I'm Digamber Singh, a New Delhi-based full-stack developer, tech author, and open-source contributor with 10+ years' experience in HTML, CSS, JavaScript, PHP, and WordPress.