An open API service indexing awesome lists of open source software.

https://github.com/namnv609/php-onesignal-sdk

PHP SDK for OneSignal RESTful API
https://github.com/namnv609/php-onesignal-sdk

notifications onesignal-php php-sdk web-push-notification

Last synced: 3 months ago
JSON representation

PHP SDK for OneSignal RESTful API

Awesome Lists containing this project

README

          

# PHP SDK for OneSignal RESTful API

> OneSignal is a high volume and reliable push notification service for websites and mobile applications. We support all major native and mobile platforms by providing dedicated SDKs for each platform, a RESTful server API, and an online dashboard for marketers to design and send push notifications.

## System requirements
* **PHP >= 5.5**

## Installation
Using Composer
`composer require namnv609/php-onesignal-sdk`
or you can include the following in your `composer.json`
`"namnv609/php-onesignal-sdk": "1.0"`
### Response format
```JSON
{"status":true,"code":200,"response":}
```
* `status`: Boolean. `true` or `false` (status code is 200 or otherwise)
* `code`: Integer. Status code
* `response`: Mixed. You can view OneSignal result detail for each API at: [https://documentation.onesignal.com/reference](https://documentation.onesignal.com/reference)

```PHP
$players = $player->all();

foreach ($players->response->players as $player) {
echo $player->id . PHP_EOL;
}
```

## Usage Instructions

First, create a new `OneSignal` instance to make configuring the library for usage.

```PHP
use NNV\OneSignal\OneSignal;

$oneSignal = new OneSignal( [, , , ])
```

Once the `OneSignal` instance has been registered. You may use it like so:

### [Application](http://namnv609.github.io/php-onesignal-sdk/class-NNV.OneSignal.API.App.html)
Application body parameters: [**Create**](https://documentation.onesignal.com/reference#create-an-app) and [**Update**](https://documentation.onesignal.com/reference#update-an-app)
```PHP
use NNV\OneSignal\API\App;

$app = new App($oneSignal);
```
* View apps
```PHP
$app->all();
```
* View an app
```PHP
$app->get("");
```
* Create an app
```PHP
$appData = [
'name' => '',
'apns_env' => 'sandbox',
];

$app->create($appData);
```
* Update an app
```PHP
$appData = [
'apns_env' => 'production',
];

$app->update("", $appData);
```

### [Player (Device)](http://namnv609.github.io/php-onesignal-sdk/class-NNV.OneSignal.API.Player.html)
Player (Device) body parameters: [**Create**](https://documentation.onesignal.com/reference#add-a-device), [**Update**](https://documentation.onesignal.com/reference#edit-device), [**New session**](https://documentation.onesignal.com/reference#new-session), [**New purchase**](https://documentation.onesignal.com/reference#new-purchase), [**Increment session length**](https://documentation.onesignal.com/reference#increment-session-length) and [**CSV export**](https://documentation.onesignal.com/reference#csv-export)
```PHP
use NNV\OneSignal\API\Player;

$player = new Player($oneSignal [, , ]);
```
* View devices
```PHP
$player->all([, ]);
```
* View device
```PHP
$player->get("");
```
* Add a device
```PHP
use NNV\OneSignal\Constants\DeviceTypes;

$playerData = [
'language' => 'en',
'tags' => [
'for' => 'bar',
'this' => 'that'
]
];

$player->create(DeviceTypes::CHROME_WEBSITE, $playerData);
```
* Edit device
```PHP
use NNV\OneSignal\Constants\NotificationTypes;
use NNV\OneSignal\Constants\TestTypes;

$playerData = [
'test_type' => TestTypes::DEVELOPMENT,
'notification_types' => NotificationTypes::UNSUBSCRIBED
];

$player->update("", $playerData);
```
* New session
```PHP
$sessionData = [
'tags' => [
'new' => 'session',
],
];
$player->onSession("", $sessionData);
```
* New purchase (Currently, i've support one item per request)
```PHP
$purchaseData = [
'sku' => 'SKU123',
'iso' => 'USD',
'amount' => '0.99',
];

$player->onPurchase("", $purchaseData, []);
```
* Increment session length
```PHP
$focusData = [
'state' => 'ping',
'active_time' => 1,
];

$player->onFocus("", $focusData);
```
* CSV export
```PHP
$extraFields = ['rooted'];

$player->csvExport($extraFields);
```

### [Notification](http://namnv609.github.io/php-onesignal-sdk/class-NNV.OneSignal.API.Notification.html)
Notification body parameters: [**Create**](https://documentation.onesignal.com/reference#create-notification)
```PHP
use NNV\OneSignal\API\Notification;

$notification = new Notification($oneSignal[, , ]);
```
* Create notification
```PHP
$notificationData = [
'included_segments' => ['All'],
'contents' => [
'en' => 'Hello, world',
],
'headings' => [
'en' => 'Hello',
],
'buttons' => [
[
'id' => 'button_id',
'text' => 'Button text',
'icon' => 'button_icon',
],
],
'filters' => [
[
'field' => 'tag',
'key' => 'level',
'relation' => '>',
'value' => '10',
],
],
'send_after' => 'Sep 24 2017 14:00:00 GMT-0700',
'isChromeWeb' => true,
];

$notification->create($notificationData);
```
* Cancel notification
```PHP
$notification->cancel("");
```
* View notification
```PHP
$notification->get("");
```
* View notifications
```PHP
$notification->all([, ]);
```
* Track open
```PHP
$notification->trackOpen("");
```