brandonhudson / onesignal-php-api
OneSignal API for PHP
Requires
- php: ^7.3
- ext-json: *
- psr/http-client: ^1.0
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1.0
- symfony/options-resolver: ^3.4|^4.0|^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- nyholm/psr7: ^1.2
- symfony/http-client: ^5.0
- symfony/phpunit-bridge: ^5.0
- dev-master / 2.0.x-dev
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.14.1
- v1.14.0
- v1.13.0
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.0
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- 1.0.x-dev
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v1.0.0-rc2
- v1.0.0-rc1
- v0.2.0
- 0.1.x-dev
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-11-07 11:55:36 UTC
README
Install
Note: All examples are for v2, if you are using PHP <7.3 please read v1 documentation.
This packages requires a PSR-18 HTTP client and PSR-17 HTTP factories to work. You can choose any from psr/http-client-implementation and psr/http-factory-implementation
Example with Symfony HttpClient and nyholm/psr7 http factories, install it with Composer:
composer require symfony/http-client nyholm/psr7 brandonhudson/onesignal-php-api
And now configure the OneSignal api client:
<?php declare(strict_types=1); use OneSignal\Config; use OneSignal\OneSignal; use Symfony\Component\HttpClient\Psr18Client; use Nyholm\Psr7\Factory\Psr17Factory; require __DIR__ . '/vendor/autoload.php'; $config = new Config('your_application_id', 'your_application_auth_key', 'your_auth_key'); $httpClient = new Psr18Client(); $requestFactory = $streamFactory = new Psr17Factory(); $oneSignal = new OneSignal($config, $httpClient, $requestFactory, $streamFactory);
How to use this library
Applications API
View the details of all of your current OneSignal applications (official documentation):
$myApps = $oneSignal->apps()->getAll();
View the details of a single OneSignal application (official documentation):
$myApp = $oneSignal->apps()->getOne('application_id');
Create a new OneSignal app (official documentation):
$newApp = $oneSignal->apps()->add([ 'name' => 'app name', 'gcm_key' => 'key' ]);
Update the name or configuration settings of OneSignal application (official documentation):
$oneSignal->apps()->update('application_id', [ 'name' => 'new app name' ]);
Create Segments (official documentation):
$oneSignal->apps()->createSegment('application_id', [ 'name' => 'Segment Name', 'filters' => [ ['field' => 'session_count', 'relation' => '>', 'value' => 1], ['operator' => 'AND'], ['field' => 'tag', 'relation' => '!=', 'key' => 'tag_key', 'value' => '1'], ['operator' => 'OR'], ['field' => 'last_session', 'relation' => '<', 'value' => '30,'], ], ]);
Delete Segments (official documentation):
$oneSignal->apps()->deleteSegment('application_id', 'segment_id');
Devices API
View the details of multiple devices in one of your OneSignal apps (official documentation):
$devices = $oneSignal->devices()->getAll();
View the details of an existing device in your configured OneSignal application (official documentation):
$device = $oneSignal->devices()->getOne('device_id');
Register a new device to your configured OneSignal application (official documentation):
use OneSignal\Api\Devices; $newDevice = $oneSignal->devices()->add([ 'device_type' => Devices::ANDROID, 'identifier' => 'abcdefghijklmn', ]);
Update an existing device in your configured OneSignal application (official documentation):
$oneSignal->devices()->update('device_id', [ 'session_count' => 2, ]);
Notifications API
View the details of multiple notifications (official documentation):
$notifications = $oneSignal->notifications()->getAll();
Get the details of a single notification (official documentation):
$notification = $oneSignal->notifications()->getOne('notification_id');
Create and send notifications or emails to a segment or individual users. You may target users in one of three ways using this method: by Segment, by Filter, or by Device (at least one targeting parameter must be specified) (official documentation):
$oneSignal->notifications()->add([ 'contents' => [ 'en' => 'Notification message' ], 'included_segments' => ['All'], 'data' => ['foo' => 'bar'], 'isChrome' => true, 'send_after' => new \DateTime('1 hour'), 'filters' => [ [ 'field' => 'tag', 'key' => 'is_vip', 'relation' => '!=', 'value' => 'true', ], [ 'operator' => 'OR', ], [ 'field' => 'tag', 'key' => 'is_admin', 'relation' => '=', 'value' => 'true', ], ], // ..other options ]);
Mark notification as opened (official documentation):
$oneSignal->notifications()->open('notification_id');
Stop a scheduled or currently outgoing notification (official documentation):
$oneSignal->notifications()->cancel('notification_id');
Notification History (official documentation):
$oneSignal->notifications()->history('notification_id', [ 'events' => 'clicked', // or 'sent' 'email' => 'your_email@email.com', ]);
Questions?
If you have any questions please open an issue.
License
This library is released under the MIT License. See the bundled LICENSE file for details.