php

How to Send Push Notifications in Laravel 9 With OneSignal

“Today, we are going to learn about the way to send push notifications in Laravel 9 with OneSignal. Before the start, we need to know what push notification is and why it’s important for your project. The name of the application, Push Notification, calls host push notification. It is a way to deliver information from your software application to a computing device without any user request. It is an important future for your software because it helps you to notify your clients with some useful information.”

OneSignal is a famous messaging service provider that allows the application to send notifications to different device platforms. Today, we will use OneSignal in our project.

Project Work Flow

Step 1- Set up a Laravel 9 Project in the Local Host

Before creating the project, ensure the Laravel environment is ready on our computer.

composer create-project laravel/laravelpush-notification-onsignal

Step 2- Connect Database With the Project

To connect the Database, we need to open the .env file from the project directory. We have to create our database user before connecting.

Step 3- Create an Account in OneSignal and get the OneSignal Authorization key

Before starting, we need to go to onesignal.com to create an account. Provide all needed information and fill up all data to create an account. Then create a new app (Select web apps).

Then you have to go to your .env file and add those lines there

ONE_SIGNAL_APP_ID=xxxxxxxxxxxxxmmm (YOUR APP ID)

ONE_SIGNAL_AUTHORIZE=xxxxxxxvvvvvv (REST API KEY)

ONE_SIGNAL_AUTH_KEY=nnncccccccc (YOUR USER AUTH KEY)

Step 4 -Install the OneSignal Package Into the Project

We need to install a laravel package to make OneSignal functional on our project.

We will use

ladumor/one-signal

Laravel package on our project.

composer require ladumor/one-signal:0.4.2

Step 5- Setup and Configure Onesignal Into Our Project

After installing the package, we have to configure it in our project.

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

The next step is needed to add Providers and aliases in our app.php under the “push-Notification/config/” file

‘providers’ =>

Ladumor\OneSignal\OneSignalServiceProvider::class,

‘aliases’ =>

'OneSignal' =>\Ladumor\OneSignal\OneSignal::class,

For sending a push notification, we need to create a controller and a function for sending a notification. We will use the sendPush() method to send notifications to our targeted person. Here is an example code

For sending Notification

publicfunctionsendNotifications()

    {

        $fieldsh['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyy'];

        $notificationMsgi = 'Hello !! It is a notification test.!';

        OneSignal::sendPush($fieldsh, $notificationMsgi);

    }

Now we need to retrieve the notification. For that, we will use our project’s getNotifications() method.

For Single Notifications received, we have to pass signal id on it. code is below

publicfunctiongetSingleNotification($notificationId){

        OneSignal::getNotification($notificationId);
    }

Or if you like to receive all Notifications, then use this code

publicfunctiongetAllNotification(){

        OneSignal::getNotifications();
    }

Step 6- Run and Test the Application by Sending a Push Notification

Finally, your project was ready for sending and receiving a push notification. Now, we need to run the Laravel project in the local server using this commend

php artisan serve

Conclusion

You can send notifications to your client via your application using this project. If you flow all instructions clearly, it should work.

About the author

Rakhibul Hasan

This is Rakhibul Hasan CEO and Founder of Clipping Path Creative Inc. We provide 100% handmade photo editing services to use Adobe Photoshop. Our services are Clipping path, Background removal, Color correction, Photo retouching, Car photo editing, drop shadow etc. Also we offer an image as a trial to judge our quality. So contact us and get instant reply.