okn / laravel-onesignal
OneSignal wrapper for Laravel
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ^6.2
- illuminate/support: 4.*|5.*|6.*
This package is auto-updated.
Last update: 2024-05-13 14:45:58 UTC
README
Introduction
This project, widely inspired by Berkayk (https://github.com/berkayk/laravel-onesignal), is another one OneSignal wrapper for Laravel. The main purpose is to make web push notifications easier. Before using this service, you'll need to complete all steps of your OneSignal setup (https://onesignal.com) to get your Application ID and API keys.
Installation
- Require package with composer
composer require okn/laravel-onesignal
- Only for Laravel version < 5.5
Add the service provider and class alias for facade support inconfig/app.php
'providers' => [ // ... Okn\OneSignal\OneSignalServiceProvider::class ]; 'aliases' => [ // ... 'OneSignal' => Okn\OneSignal\Facades\OneSignal::class ];
- Run artisan command to install the service
php artisan onesignal:install
This command will create a default config file config/onesignal.php
.
Configuration
App ID et API keys must be defined in the .env
file as follow:
ONESIGNAL_APP_ID= ONESIGNAL_REST_API_KEY= ONESIGNAL_USER_AUTH_KEY=
Usage
Default use
- Create a notification
$notification = OneSignal::createNotification([ 'headings'=>'Title', 'contents'=>'Notification message...', 'url'=>'https://yourwebsite.com' ]);
- Send notification
- To segment(s)
$notification->send(['segments'=>['SEGMENT-NAME']]);
- To specific user(s)
$notification->send(['ids'=>['PLAYER-ID-1','PLAYER-ID-2','PLAYER-ID-3']]);
- Asynchronously
$promise = $notification->async()->send([$params]);
This will return a GuzzleHttp\Promise\Promise
(http://docs.guzzlephp.org/en/stable/quickstart.html#async-requests)
Add buttons to notification
$notification->withButtons([ [ 'id' => 'btnId1', 'text' => 'Webpush button test', 'icon' => 'https://yourwebsite.com/images/icon1.png', 'url' => 'https://yourwebsite.com/action1' ], [ 'id' => 'btnId2', 'text' => 'Webpush button test #2', 'icon' => 'https://yourwebsite.com/images/icon2.png', 'url' => 'https://yourwebsite.com/action2' ] ])->send([$params]);
Send a default template test notification
- To an existing segment named "Admin"
OneSignal::test();
This method also accepts the same argument as the method send()
.
Retrieve Users
- All users
OneSignal::getUsers(300, 0);
The first argument is the maximum limit, and the second is the offset. Both are optional.
- Specific user
OneSignal::getUser('PLAYER-ID');
Exception(s)
cURL error 60: SSL certificate problem...
cURL need an SSL certificate to communicate through https protocol.
Solution 1 (recommended)
Install an SSL certificate on your local machine
Assuming you are using WAMP on Windows:
- download an SSL certificate for your local server https://curl.haxx.se/ca/cacert.pem
- put it in your prefered directory (mine is
C:\Users\[MY-USERNAME]\cacert.pem
) - edit this variable in your
php.ini
to add the path to the certificate
curl.cainfo = "C:\Users\[MY-USERNAME]\cacert.pem"
- restart your webserver
Now it should works, if it doesn't you might try the next solution.
Solution 2
Disable SSL validation (not recommended)
In .env
file you can add the following line:
ONESIGNAL_SSL_VERIFY=false