kodjunkie / onesignal-php-sdk by kodjunkie

OneSignal SDK for PHP developers with fluent API and supports Laravel / Lumen out of the box.
1,432
3
2
Package Data
Maintainer Username: kodjunkie
Maintainer Contact: paplow01@gmail.com (Lawrence Onah)
Package Create Date: 2021-08-04
Package Last Update: 2023-02-03
Home Page: https://packagist.org/packages/kodjunkie/onesignal-php-sdk
Language: PHP
License: MIT
Last Refreshed: 2024-04-23 03:09:37
Package Statistics
Total Downloads: 1,432
Monthly Downloads: 21
Daily Downloads: 2
Total Stars: 3
Total Watchers: 2
Total Forks: 2
Total Open Issues: 0

OneSignal SDK for PHP developers with fluent API and supports Laravel / Lumen out of the box.

Latest Version on Packagist tests

Installation

composer require kodjunkie/onesignal-php-sdk

NOTE: For Laravel users, this package registers itself automatically.

Usage in plain PHP

use Kodjunkie\OnesignalPhpSdk\OneSignal;
use Kodjunkie\OnesignalPhpSdk\Exceptions\OneSignalException;

try {
    $config = [
        // Onesignal API key
        'api_key' => '',
        // Onesignal Auth key
        'auth_key' => '',
        // Onesignal App ID (optional)
        // Providing your app_id is beneficial if you're working with a single OneSignal app
        // and don't want to provide it all the time for endpoints that requires it
        'app_id' => '',
    ];
    
    // Initialize the SDK
    $oneSignal = new OneSignal($config);
    
    // Using the API
    // Get all apps
    $response = $oneSignal->app()->getAll();
    
    // You can use json_decode to get the response as an stdClass Object
    var_dump($response);
} catch (OneSignalException $exception) {
    var_dump($exception->getMessage());
}

Usage in Laravel / Lumen

Set these values in your .env file

ONESIGNAL_API_KEY=
ONESIGNAL_AUTH_KEY=
ONESIGNAL_APP_ID=

Register the service provider (lumen users only)

Add this line to your bootstrap/app.php file

$app->register(\Kodjunkie\OnesignalPhpSdk\OneSignalServiceProvider::class);

Lastly, use in your controller or wherever it's needed

use Kodjunkie\OnesignalPhpSdk\OneSignal;
use Kodjunkie\OnesignalPhpSdk\Exceptions\OneSignalException;

try {
    // Initialize the SDK
    // Resolve from the IoC container
    $oneSignal = app()->make('onesignal');
    
    // Using the API
    // Get all devices
    $response = $oneSignal->device()->getAll($appId, $limit, $offset);
    
    // Using Facade, the code above will look like this
    // With app_id provided in the config
    $response = OneSignal::device()->getAll(null, $limit, $offset);
    
    dd($response);
} catch (OneSignalException $exception) {
    dd($exception->getMessage());
}

Tests

composer test

License

This project is opened under the MIT 2.0 License which allows very broad use for both academic and commercial purposes.