|
8 | 8 | [](https://styleci.io/repos/34352212)
|
9 | 9 | [](https://packagist.org/packages/norkunas/onesignal-php-api)
|
10 | 10 |
|
11 |
| -## Documentation |
| 11 | +## Install |
12 | 12 |
|
13 |
| -[Read the Documentation for 1.0](/docs/getting-started.md) |
| 13 | +This packages requires an http adapter to work. You can choose any from |
| 14 | +[php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation) |
14 | 15 |
|
15 |
| -[Read the Documentation for 0.1](https://github.com/norkunas/onesignal-php-api/blob/0.1/README.md) |
| 16 | +Example with Guzzle v6 adapter, install it with [Composer](https://getcomposer.org/): |
| 17 | + |
| 18 | +``` |
| 19 | +composer require php-http/guzzle6-adapter norkunas/onesignal-php-api |
| 20 | +``` |
| 21 | + |
| 22 | +And now configure the service: |
| 23 | + |
| 24 | +```php |
| 25 | +<?php |
| 26 | + |
| 27 | +require __DIR__ . '/vendor/autoload.php'; |
| 28 | + |
| 29 | +use GuzzleHttp\Client as GuzzleClient; |
| 30 | +use Http\Adapter\Guzzle6\Client as GuzzleAdapter; |
| 31 | +use Http\Client\Common\HttpMethodsClient as HttpClient; |
| 32 | +use Http\Message\MessageFactory\GuzzleMessageFactory; |
| 33 | +use OneSignal\Config; |
| 34 | +use OneSignal\OneSignal; |
| 35 | + |
| 36 | +$config = new Config(); |
| 37 | +$config->setApplicationId('your_application_id'); |
| 38 | +$config->setApplicationAuthKey('your_application_auth_key'); |
| 39 | +$config->setUserAuthKey('your_auth_key'); |
| 40 | + |
| 41 | +$guzzle = new GuzzleClient([ // http://docs.guzzlephp.org/en/stable/quickstart.html |
| 42 | + // ..config |
| 43 | +]); |
| 44 | + |
| 45 | +$client = new HttpClient(new GuzzleAdapter($guzzle), new GuzzleMessageFactory()); |
| 46 | +$api = new OneSignal($config, $client); |
| 47 | +``` |
| 48 | + |
| 49 | +## How to use this library |
| 50 | + |
| 51 | +### Applications API |
| 52 | + |
| 53 | +View the details of all of your current OneSignal applications ([official documentation](https://documentation.onesignal.com/reference#view-apps-apps)): |
| 54 | + |
| 55 | +```php |
| 56 | +$myApps = $api->apps->getAll(); |
| 57 | +``` |
| 58 | + |
| 59 | +View the details of a single OneSignal application ([official documentation](https://documentation.onesignal.com/reference#view-an-app)): |
| 60 | + |
| 61 | +```php |
| 62 | +$myApp = $api->apps->getOne('application_id'); |
| 63 | +``` |
| 64 | + |
| 65 | +Create a new OneSignal app ([official documentation](https://documentation.onesignal.com/reference#create-an-app)): |
| 66 | + |
| 67 | +```php |
| 68 | +$newApp = $api->apps->add([ |
| 69 | + 'name' => 'app name', |
| 70 | + 'gcm_key' => 'key' |
| 71 | +]); |
| 72 | +``` |
| 73 | + |
| 74 | +Update the name or configuration settings of OneSignal application ([official documentation](https://documentation.onesignal.com/reference#update-an-app)): |
| 75 | + |
| 76 | +```php |
| 77 | +$api->apps->update('application_id', [ |
| 78 | + 'name' => 'new app name' |
| 79 | +]); |
| 80 | +``` |
| 81 | + |
| 82 | +### Devices API |
| 83 | + |
| 84 | +View the details of multiple devices in one of your OneSignal apps ([official documentation](https://documentation.onesignal.com/reference#view-devices)): |
| 85 | + |
| 86 | +```php |
| 87 | +$devices = $api->devices->getAll(); |
| 88 | +``` |
| 89 | + |
| 90 | +View the details of an existing device in your configured OneSignal application ([official documentation](https://documentation.onesignal.com/reference#view-device)): |
| 91 | + |
| 92 | +```php |
| 93 | +$device = $api->devices->getOne('device_id'); |
| 94 | +``` |
| 95 | + |
| 96 | +Register a new device to your configured OneSignal application ([official documentation](https://documentation.onesignal.com/reference#add-a-device)): |
| 97 | + |
| 98 | +```php |
| 99 | +$newDevice = $api->devices->add([ |
| 100 | + 'device_type' => Devices::ANDROID, |
| 101 | + 'identifier' => 'abcdefghijklmn', |
| 102 | +]); |
| 103 | +``` |
| 104 | + |
| 105 | +Update an existing device in your configured OneSignal application ([official documentation](https://documentation.onesignal.com/reference#edit-device)): |
| 106 | + |
| 107 | +```php |
| 108 | +$api->devices->update('device_id', [ |
| 109 | + 'session_count' => 2, |
| 110 | +]); |
| 111 | +``` |
| 112 | + |
| 113 | +### Notifications API |
| 114 | + |
| 115 | +View the details of multiple notifications ([official documentation](https://documentation.onesignal.com/reference#view-notifications)): |
| 116 | + |
| 117 | +```php |
| 118 | +$notifications = $api->notifications->getAll(); |
| 119 | +``` |
| 120 | + |
| 121 | +Get the details of a single notification ([official documentation](https://documentation.onesignal.com/reference#view-notification)): |
| 122 | + |
| 123 | +```php |
| 124 | +$notification = $api->notifications->getOne('notification_id'); |
| 125 | +``` |
| 126 | + |
| 127 | +Create and send notifications or emails to a segment or individual users. |
| 128 | +You may target users in one of three ways using this method: by Segment, by |
| 129 | +Filter, or by Device (at least one targeting parameter must be specified) ([official documentation](https://documentation.onesignal.com/reference#create-notification)): |
| 130 | + |
| 131 | +```php |
| 132 | +$api->notifications->add([ |
| 133 | + 'contents' => [ |
| 134 | + 'en' => 'Notification message' |
| 135 | + ], |
| 136 | + 'included_segments' => ['All'], |
| 137 | + 'data' => ['foo' => 'bar'], |
| 138 | + 'isChrome' => true, |
| 139 | + 'send_after' => new \DateTime('1 hour'), |
| 140 | + 'filters' => [ |
| 141 | + [ |
| 142 | + 'field' => 'tag', |
| 143 | + 'key' => 'is_vip', |
| 144 | + 'relation' => '!=', |
| 145 | + 'value' => 'true', |
| 146 | + ], |
| 147 | + [ |
| 148 | + 'operator' => 'OR', |
| 149 | + ], |
| 150 | + [ |
| 151 | + 'field' => 'tag', |
| 152 | + 'key' => 'is_admin', |
| 153 | + 'relation' => '=', |
| 154 | + 'value' => 'true', |
| 155 | + ], |
| 156 | + ], |
| 157 | + // ..other options |
| 158 | +])); |
| 159 | +``` |
| 160 | + |
| 161 | +Mark notification as opened ([official documentation](https://documentation.onesignal.com/reference#track-open)): |
| 162 | + |
| 163 | +```php |
| 164 | +$api->notifications->open('notification_id'); |
| 165 | +``` |
| 166 | + |
| 167 | +Stop a scheduled or currently outgoing notification ([official documentation](https://documentation.onesignal.com/reference#cancel-notification)): |
| 168 | + |
| 169 | +```php |
| 170 | +$api->notifications->cancel('notification_id'); |
| 171 | +``` |
| 172 | + |
| 173 | +## Questions? |
| 174 | + |
| 175 | +If you have any questions please [open an issue](https://github.com/norkunas/onesignal-php-api/issues/new). |
16 | 176 |
|
17 | 177 | ## License
|
18 | 178 |
|
|
0 commit comments