“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
- Step 2- Connect Database with the project
- Step 3- Create an account in OneSignal and get the OneSignal Authorization key
- Step 4 -Install the OneSignal package into the project
- Step 5- Setup and configure OneSignal into our project
- Step 6- Run and Test the application by sending a push notification
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.
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
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
Laravel package on our project.
Step 5- Setup and Configure Onesignal Into Our Project
After installing the package, we have to configure it in our project.
The next step is needed to add Providers and aliases in our app.php under the “push-Notification/config/” file
‘providers’ =>
‘aliases’ =>
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
{
$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
OneSignal::getNotification($notificationId);
}
Or if you like to receive all Notifications, then use this code
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
Conclusion
You can send notifications to your client via your application using this project. If you flow all instructions clearly, it should work.