Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit bcdaf4a

Browse files
committed
Pre-release version 1.0
0 parents  commit bcdaf4a

File tree

10 files changed

+1457
-0
lines changed

10 files changed

+1457
-0
lines changed

README.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# PHP SDK for OneSignal RESTful API
2+
3+
> 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.
4+
5+
## System requirements
6+
* **PHP >= 5.4**
7+
8+
## Installation
9+
Using Composer
10+
`composer install namnv609/php-onesignal-sdk`
11+
or you can include the following in your `composer.json`
12+
`"namnv609/php-onesignal-sdk": "1.0"`
13+
### Response format
14+
```JSON
15+
{"status":true,"code":200,"response":<OneSignal result>}
16+
```
17+
* `status`: Boolean. `true` or `false` (status code is 200 or otherwise)
18+
* `code`: Integer. Status code
19+
* `response`: Mixed. You can view OneSignal result detail for each API at: [https://documentation.onesignal.com/reference](https://documentation.onesignal.com/reference)
20+
21+
```PHP
22+
$players = $player->all();
23+
24+
foreach ($players->response->players as $player) {
25+
echo $player->id . PHP_EOL;
26+
}
27+
```
28+
29+
## Usage Instructions
30+
31+
First, create a new `OneSignal` instance to make configuring the library for usage.
32+
33+
```PHP
34+
use NNV\OneSignal\OneSignal;
35+
36+
$oneSignal = new OneSignal(<User Auth key> [, <App ID>, <App REST key>, <Extra options for GuzzleHttp Client>])
37+
```
38+
39+
Once the `OneSignal` instance has been registered. You may use it like so:
40+
41+
### [Application](http://namnv609.github.io/php-onesignal-sdk/class-NNV.OneSignal.API.App.html)
42+
Application body parameters: [**Create**](https://documentation.onesignal.com/reference#create-an-app) and [**Update**](https://documentation.onesignal.com/reference#update-an-app)
43+
```PHP
44+
use NNV\OneSignal\API\App;
45+
46+
$app = new App($oneSignal);
47+
```
48+
* View apps
49+
```PHP
50+
$app->all();
51+
```
52+
* View an app
53+
```PHP
54+
$app->get("<App ID>");
55+
```
56+
* Create an app
57+
```PHP
58+
$appData = [
59+
'name' => '<App name>',
60+
'apns_env' => 'sandbox',
61+
];
62+
63+
$app->create($appData);
64+
```
65+
* Update an app
66+
```PHP
67+
$appData = [
68+
'apns_env' => 'production',
69+
];
70+
71+
$app->update("<App ID>", $appData);
72+
```
73+
74+
### [Player (Device)](http://namnv609.github.io/php-onesignal-sdk/class-NNV.OneSignal.API.Player.html)
75+
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)
76+
```PHP
77+
use NNV\OneSignal\API\Player;
78+
79+
$player = new Player($oneSignal [, <App ID>, <App REST key>]);
80+
```
81+
* View devices
82+
```PHP
83+
$player->all([<Limit>, <Offset>]);
84+
```
85+
* View device
86+
```PHP
87+
$player->get("<Player ID>");
88+
```
89+
* Add a device
90+
```PHP
91+
use NNV\OneSignal\Constants\DeviceTypes;
92+
93+
$playerData = [
94+
'language' => 'en',
95+
'tags' => [
96+
'for' => 'bar',
97+
'this' => 'that'
98+
]
99+
];
100+
101+
$player->create(DeviceTypes::CHROME_WEBSITE, $playerData);
102+
```
103+
* Edit device
104+
```PHP
105+
use NNV\OneSignal\Constants\NotificationTypes;
106+
use NNV\OneSignal\Constants\TestTypes;
107+
108+
$playerData = [
109+
'test_type' => TestTypes::DEVELOPMENT,
110+
'notification_types' => NotificationTypes::UNSUBSCRIBED
111+
];
112+
113+
$player->update("<Player ID>", $playerData);
114+
```
115+
* New session
116+
```PHP
117+
$sessionData = [
118+
'tags' => [
119+
'new' => 'session',
120+
],
121+
];
122+
$player->onSession("<Player ID>", $sessionData);
123+
```
124+
* New purchase (Currently, i've support one item per request)
125+
```PHP
126+
$purchaseData = [
127+
'sku' => 'SKU123',
128+
'iso' => 'USD',
129+
'amount' => '0.99',
130+
];
131+
132+
$player->onPurchase("<Player ID>", $purchaseData, [<Is existing>]);
133+
```
134+
* Increment session length
135+
```PHP
136+
$focusData = [
137+
'state' => 'ping',
138+
'active_time' => 1,
139+
];
140+
141+
$player->onFocus("<App ID>", $focusData);
142+
```
143+
* CSV export
144+
```PHP
145+
$extraFields = ['rooted'];
146+
147+
$player->csvExport($extraFields);
148+
```
149+
150+
### [Notification](http://namnv609.github.io/php-onesignal-sdk/class-NNV.OneSignal.API.Notification.html)
151+
Notification body parameters: [**Create**](https://documentation.onesignal.com/reference#create-notification)
152+
```PHP
153+
use NNV\OneSignal\API\Notification;
154+
155+
$notification = new Notification($oneSignal[, <App ID>, <App REST key>]);
156+
```
157+
* Create notification
158+
```PHP
159+
$notificationData = [
160+
'included_segments' => ['All'],
161+
'contents' => [
162+
'en' => 'Hello, world',
163+
],
164+
'headings' => [
165+
'en' => 'Hello',
166+
],
167+
'buttons' => [
168+
[
169+
'id' => 'button_id',
170+
'text' => 'Button text',
171+
'icon' => 'button_icon',
172+
],
173+
],
174+
'filters' => [
175+
[
176+
'field' => 'tag',
177+
'key' => 'level',
178+
'relation' => '>',
179+
'value' => '10',
180+
],
181+
],
182+
'send_after' => 'Sep 24 2017 14:00:00 GMT-0700',
183+
'isChromeWeb' => true,
184+
];
185+
186+
$notification->create($notificationData);
187+
```
188+
* Cancel notification
189+
```PHP
190+
$notification->cancel("<Notification ID>");
191+
```
192+
* View notification
193+
```PHP
194+
$notification->get("<Notification ID>");
195+
```
196+
* View notifications
197+
```PHP
198+
$notification->all([<Limit>, <Offset>]);
199+
```
200+
* Track open
201+
```PHP
202+
$notification->trackOpen("<Notification ID>");
203+
```

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "namnv609/php-onesignal-sdk",
3+
"description": "PHP SDK for OneSignal RESTful API",
4+
"keywords": ["onesignal", "web push notification", "notification", "restful api"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "NamNV609",
10+
"email": "namnv609@gmail.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"NNV\\OneSignal\\": "src/"
16+
}
17+
},
18+
"minimum-stability": "dev",
19+
"require": {
20+
"guzzlehttp/guzzle": "^6.1",
21+
"php": ">=5.4",
22+
"symfony/options-resolver": "^3.2"
23+
}
24+
}

src/API/App.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace NNV\OneSignal\API;
4+
5+
use NNV\OneSignal\OneSignal;
6+
use NNV\OneSignal\Utils\Validation;
7+
8+
/**
9+
* Application API
10+
*/
11+
class App
12+
{
13+
/**
14+
* OneSignal instance
15+
*
16+
* @var NNV\OneSignal\OneSignal
17+
*/
18+
private $oneSignal;
19+
20+
/**
21+
* @param NNV\OneSignal\OneSignal $oneSignal OneSignal instance
22+
*/
23+
public function __construct(OneSignal $oneSignal)
24+
{
25+
$this->oneSignal = $oneSignal;
26+
}
27+
28+
/**
29+
* Get all apps
30+
*
31+
* @return NNV\OneSignal\OneSignal::execute()
32+
*/
33+
public function all()
34+
{
35+
$apps = $this->oneSignal->execute('apps', 'GET');
36+
37+
return $apps;
38+
}
39+
40+
/**
41+
* Find app by App ID
42+
*
43+
* @param string $id App ID
44+
* @return NNV\OneSignal\OneSignal::execute()
45+
*/
46+
public function get($id)
47+
{
48+
$url = sprintf('apps/%s', $id);
49+
$app = $this->oneSignal->execute($url, 'GET');
50+
51+
return $app;
52+
}
53+
54+
/**
55+
* Create app
56+
*
57+
* @param array $appData App information
58+
* @return NNV\OneSignal\OneSignal::execute()
59+
*/
60+
public function create(array $appData)
61+
{
62+
$this->validateAppData($appData, $this->getAppDataRules());
63+
64+
$app = $this->oneSignal->execute('apps', 'POST', [
65+
'form_params' => $appData,
66+
]);
67+
68+
return $app;
69+
}
70+
71+
/**
72+
* Update an app
73+
*
74+
* @param string $appId App ID
75+
* @param array $appData App data to update
76+
* @return NNV\OneSignal\OneSignal::execute()
77+
*/
78+
public function update($appId, array $appData)
79+
{
80+
$appDataRules = $this->getAppDataRules();
81+
unset($appDataRules['required'][0]);
82+
83+
$this->validateAppData($appData, $appDataRules);
84+
85+
$url = sprintf('apps/%s', $appId);
86+
$app = $this->oneSignal->execute($url, 'PUT', [
87+
'form_params' => $appData,
88+
]);
89+
90+
return $app;
91+
}
92+
93+
/**
94+
* Application data validation rules
95+
*
96+
* @return array Validation rules
97+
*/
98+
private function getAppDataRules()
99+
{
100+
return [
101+
'required' => ['name'],
102+
'defined' => [
103+
'name' => 'string',
104+
'apns_env' => [
105+
'allowedTypes' => 'string',
106+
'allowedValues' => ['sandbox', 'production'],
107+
],
108+
'apns_p12' => 'string',
109+
'apns_p12_password' => 'string',
110+
'gcm_key' => 'string',
111+
'android_gcm_sender_id' => 'string',
112+
'chrome_web_origin' => 'string',
113+
'chrome_web_origin' => 'string',
114+
'chrome_web_default_notification_icon' => 'string',
115+
'chrome_web_sub_domain' => 'string',
116+
'safari_apns_p12' => 'string',
117+
'safari_apns_p12_password' => 'string',
118+
'site_name' => 'string',
119+
'safari_site_origin' => 'string',
120+
'safari_icon_256_256' => 'string',
121+
'chrome_key' => 'string',
122+
],
123+
];
124+
}
125+
126+
/**
127+
* Validate application data
128+
*
129+
* @param array $appData Application data
130+
* @param array $validateRules Application validation rules
131+
* @throws mixed NNV\OneSignal\Utils\Validation::validate or null
132+
*/
133+
private function validateAppData(array $appData, array $validateRules)
134+
{
135+
$validation = new Validation;
136+
137+
$validation->setMultiRequired($validateRules['required'])
138+
->setMultiDefined($validateRules['defined'])
139+
->validate($appData);
140+
}
141+
}

0 commit comments

Comments
 (0)