The OneSingnal is the market leader in push notification providers. It provides mobile + web push, email & in-app messages, and an easy way to send notifications. OneSignal provides official core PHP APIs but not the Laravel package. We are using OneSignal in many projects and write a bunch of line code in all projects where we needed OneSingnal.
One day I had an idea in my mind why I should not write a Laravel wrapper for OneSignal?. Finally, I wrote the shailesh-ladumor/one-signal Laravel Wrapper for it. Using this package, we can write neat & clean code and just a few lines of code.
OneSignal add this package in his official docs here
You can watch the following video tutorial or follow the article.
This package also works with the previous Laravel version.
Today we are going to see how we can use Laravel OneSignal Wrapper in Laravel. Let's see step by step, how we can do that.
Spet 1: Install Packages
Install shailesh-ladumor/one-signal by the following command,
composer require ladumor/one-signal
Step 2: Publish the config file
Run the following command to publish config file,
php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider"
Step 3: Add Provider
Add the provider to your config/app.php into the provider section if using a lower version of Laravel,
Step 4: Add Facade
Add the Facade to your config/app.php into aliases section,
'OneSignal' => \Ladumor\OneSignal\OneSignal::class
Configure a .env file with following keys
ONE_SIGNAL_APP_ID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX ONE_SIGNAL_AUTHORIZE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX X ONE_SIGNAL_AUTH_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ONE_SIGNAL_AUTH_KEY is optional if you do not want to create an app. I hope you are familiar with the One Signal Platform and know how to get APP_ID and AUTHORIZE. If not, you should see the below image for how to get it.
So, we are done. Let's check how to send push notifications.
Check out this code to send a push notification.
use Ladumor\OneSignal\OneSignal; $fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyy'] $message = 'hey!! This is a test push.!' OneSignal::sendPush($fields, $message);