OneSignal Web Push Notifications for Laravel

Berkay Kaya
Berkay Kaya
Published in
2 min readMay 7, 2016

Push notifications for the web are becoming much popular these days, especially when it has become heavily used by Facebook and WhatsApp Web. In terms of notifying the user about a message, comment, etc. they are pretty useful.

In one of my Android applications, I used OneSignal and I was quite happy with it. Then I decided to create a package that I can use OneSignal for my web projects.

You may download the package from packagist and this is the GitHub link.

It’s a simple service including many helper methods. With this package, you can easily

  • send notification to all users
  • target a specific segment
  • target a specific user
  • send a custom notification with custom parameters

I am planning to extend this package with traits and additional helper methods later on.

Installation

Before you start installing this package, please complete your OneSignal setup at https://onesignal.com and finish all the steps that is necessary to obtain an application id and REST API Keys.

First, you’ll need to require the package with Composer:

composer require berkayk/onesignal-laravel

Aftwards, run composer update from your command line.

Then, update config/app.php by adding an entry for the service provider.

'providers' => [ // ... Berkayk\OneSignal\OneSignalServiceProvider::class ];

Then, register class alias by adding an entry in aliases section

'aliases' => [ // ... 'OneSignal' => Berkayk\OneSignal\OneSignalFacade::class ];

Finally, from the command line, run

php artisan vendor:publish

to publish the default configuration file. This will publish a configuration file named onesignal.php which includes your OneSignal authorization keys.

Configuration

You need to fill in onesignal.php file that is found in your applications config directory.
app_id is your OneSignal App ID and rest_api_key is your REST API Key.

Sending a Notification To All Users

You can easily send a message to all registered users with the command

OneSignal::sendNotificationToAll("Some Message", $url = null, $data = null);

$url and $data fields are optional. If you provide a $url parameter, users will be redirecting to that url.

Sending a Notification To A Specific User

After storing a user’s tokens in a table, you can simply send a message with

OneSignal::sendNotificationToUser("Some Message", $userId, $url = null, $data = null);

$userId is the user’s unique id where he/she is registered for notifications. Read https://documentation.onesignal.com/docs/website-sdk-api#getUserId for additional details.

Sending a Notification To Segment

You can simply send a notification to a specific segment with

OneSignal::sendNotificationToSegment("Some Message", $segment, $url = null, $data = null);

Sending a Custom Notification

You can send a custom message with

OneSignal::sendNotificationCustom($parameters);

The example below will send a delayed message to a user at his/her timezone at 12:25PM.

$userId = "4bc5da02-1722-4fee-943d-c8b5ccd507a2"; 
$params = [];
$params['include_player_ids'] = array($userId);
$contents = [
"en" => "Some English Message",
"tr" => "Some Turkish Message"
];
$params['contents'] = $contents;
$params['delayed_option'] = "timezone"; // Will deliver on user's timezone
$params['delivery_time_of_day'] = "12:25PM"; // Delivery time

For additional documentation, you may refer to https://documentation.onesignal.com/docs/notifications-create-notification

I really hope that this package helps you in your projects. Thanks for reading.

Originally published at berkaykaya.com on May 7, 2016.

--

--