├── .github └── FUNDING.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── _config.yml ├── composer.json ├── config └── one-signal.php ├── src ├── OneSignal.php ├── OneSignalClient.php ├── OneSignalManager.php ├── OneSignalServiceProvider.php ├── commands │ └── PublishUserDevice.php └── stubs │ ├── CreateUserDeviceTable.stub │ ├── UserDevice.stub │ ├── UserDeviceAPIController.stub │ └── UserDeviceRepository.stub └── v1.0.0-guide.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | custom: https://www.buymeacoffee.com/ladumor 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | .env.*.php 3 | .env.php 4 | .env 5 | .idea -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### [v2.0.0](https://github.com/shailesh-ladumor/one-signal/releases/tag/v2.0.0) (Jan 13, 2025) 2 | #### Added 3 | * One Signal API Latest version support add 4 | * New APIs Support added 5 | * Live Activities 6 | * User 7 | * Subscriptions 8 | * Templates 9 | * Laravel 11 support add 10 | * User devices new fields added as per user modal 11 | * Laravel 7 and below support removed (use version 1.0.0 this versions) 12 | * Deperecated api support removed 13 | * Devices 14 | 15 | ### [v1.0.0](https://github.com/shailesh-ladumor/one-signal/releases/tag/v1.0.0) (Jun 22, 2023) 16 | #### Added 17 | * Laravel 10 support add 18 | 19 | ### [v0.4.4](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.4.4) (Feb 1, 2023) 20 | #### Added 21 | * add [View Outcomes](https://documentation.onesignal.com/reference/view-outcomes) support 22 | 23 | ### [v0.4.3](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.4.3) (Feb 27, 2022) 24 | 25 | #### Fixed 26 | * fixed bug: The migration is thrown error (Laravel 8.83.23) : PHP Fatal error: Cannot declare class CreateUserDevicesTable, [(#21)](https://github.com/shailesh-ladumor/one-signal/pull/21) 27 | 28 | ### [v0.4.2](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.4.2) (Feb 27, 2022) 29 | 30 | #### Added 31 | * Laravel 9 support add 32 | * Delete Device Support Add 33 | 34 | ### [v0.4.0](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.4.0) (Jun 21, 2021) 35 | 36 | #### Added 37 | * Add User Device Mobile API support [(#12)](https://github.com/shailesh-ladumor/one-signal/pull/12) [(#7a3243d)](https://github.com/shailesh-ladumor/one-signal/commit/7a3243d30ab64e05fed5edea22386a3ea3a6ff74) 38 | * Readme updated 39 | 40 | ### [v0.3.4](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.3.4) (Dec 25, 2020) 41 | 42 | ##### Changed 43 | * Laravel v8.x Support add [(#8)](https://github.com/shailesh-ladumor/one-signal/pull/8) [(ef044f)](https://github.com/shailesh-ladumor/one-signal/pull/8/commits/42e34111340519ebf9104ea0f5a3c49892ef044f) 44 | * fixed issue [#7](https://github.com/shailesh-ladumor/one-signal/issues/7) 45 | 46 | ### [v0.3.3](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.3.3) (July 18, 2020) 47 | 48 | ##### Changed 49 | * Fix the update App API and moved config vars into client [(#6)](https://github.com/shailesh-ladumor/one-signal/pull/6) [(98647a3)](https://github.com/shailesh-ladumor/one-signal/pull/6/commits/98647a35d7b69d8f63adad803278a85f8244b6ed) 50 | * Readme updated 51 | 52 | ### [v0.3.2](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.3.2) (May 10, 2020) 53 | 54 | ##### Added 55 | * Segments support add [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/5) [(988e474)](https://github.com/shailesh-ladumor/one-signal/commit/988e4741249a37d076c08ff902dadf84c399a77e) 56 | * Apps Support add [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/5) [(7660ac3)](https://github.com/shailesh-ladumor/one-signal/commit/7660ac39baa55d2eb60610ed50192a80d3e086ee) 57 | ##### Changed 58 | * Readme updated with `apps` and `segments` example [(#5)](https://github.com/shailesh-ladumor/one-signal/pull/5) 59 | 60 | ### [v0.3.1](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.3.1) (April 4th, 2020) 61 | ##### Changed 62 | * `Device` URl change [issue reported by @yusronMu77] [(#3)](https://github.com/shailesh-ladumor/one-signal/issues/3) [(0986551)](https://github.com/shailesh-ladumor/one-signal/commit/0986551481e395da25aef4f6895f63a0e0c4e914) 63 | 64 | ### [v0.3.0](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.3.0) (March 15th, 2020) 65 | 66 | ##### Added 67 | * `Laravel 7` support added [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/2) [(cd3f347)](https://github.com/shailesh-ladumor/one-signal/pull/2/commits/cd3f347e4ee0cf1cd9101c2dae599713e9d650f7) 68 | * Retrieve all `devices` support added [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/2) [(f947ea4)](https://github.com/shailesh-ladumor/one-signal/pull/2/commits/f947ea4bbe3dcf14aaefd0942eff07375e818c56#diff-21b615b0e4f9bb5208699be064b7d31bR174) 69 | * Retrieve single `device` support added [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/2) [(f947ea4)](https://github.com/shailesh-ladumor/one-signal/pull/2/commits/f947ea4bbe3dcf14aaefd0942eff07375e818c56#diff-21b615b0e4f9bb5208699be064b7d31bR188) 70 | * Create `device` support added [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/2) [(f947ea4)](https://github.com/shailesh-ladumor/one-signal/pull/2/commits/f947ea4bbe3dcf14aaefd0942eff07375e818c56#diff-21b615b0e4f9bb5208699be064b7d31bR202) 71 | * update `device` support added [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/2) [(f947ea4)](https://github.com/shailesh-ladumor/one-signal/pull/2/commits/f947ea4bbe3dcf14aaefd0942eff07375e818c56#diff-21b615b0e4f9bb5208699be064b7d31bR223) 72 | ##### Changed 73 | * Readme updated with `devices` example [(#2)](https://github.com/shailesh-ladumor/one-signal/pull/2) 74 | 75 | ### [v0.2.0](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.2.0) (Feb 2nd, 2020) 76 | 77 | ##### Added 78 | 79 | * Retrieve all notifications support added [(#1)](https://github.com/shailesh-ladumor/one-signal/pull/1) [(6d54af9)](https://github.com/shailesh-ladumor/one-signal/pull/1/commits/6d54af94f134a541ddbc02f9c823097093fce72a) 80 | * Retrieve single notifications support added [(#1)](https://github.com/shailesh-ladumor/one-signal/pull/1) [(fbc593f)](https://github.com/shailesh-ladumor/one-signal/pull/1/commits/fbc593fd4aa2ff63d194d84bbe64c6ce1b86a27b) 81 | * Changelog added [(#1)](https://github.com/shailesh-ladumor/one-signal/pull/1) [(a189100)](https://github.com/shailesh-ladumor/one-signal/pull/1/commits/a189100566076e66459f5768affbaaa0c907421e) 82 | * notifications example added in readme [(#1)](https://github.com/shailesh-ladumor/one-signal/pull/1) [(46172de)](https://github.com/shailesh-ladumor/one-signal/pull/1/commits/46172deffa10fb973fdfb1ea074636a902eadfd8) 83 | 84 | #### Changed 85 | * Readme updated with contents [(#1)](https://github.com/shailesh-ladumor/one-signal/pull/1) [(1e877bd)](https://github.com/shailesh-ladumor/one-signal/pull/1/commits/1e877bdbe154eeb36c384c4aa4d3827d65764779) 86 | 87 | ### [v0.1.0](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.1.0) (Jan 31st, 2020) 88 | 89 | ##### Added 90 | 91 | * Send Push support added 92 | * Customize content support added 93 | * Readme - docs added 94 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Shailesh Ladumor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel One Signal 2 | 3 | Laravel One Signal is Laravel Wrapper for [One Signal](https://onesignal.com). One Signal is a great platform for send a 4 | push notification to your users. This package mentions in One Signal's official Document. you can 5 | see [here](https://documentation.onesignal.com/docs/other-cms-setup) 6 | 7 | [![Total Downloads](https://poser.pugx.org/ladumor/one-signal/downloads)](https://packagist.org/packages/ladumor/one-signal) 8 | [![Daily Downloads](https://poser.pugx.org/ladumor/one-signal/d/daily)](https://packagist.org/packages/ladumor/one-signal) 9 | [![Monthly Downloads](https://poser.pugx.org/ladumor/one-signal/d/monthly)](https://packagist.org/packages/ladumor/one-signal) 10 | [![License](https://poser.pugx.org/ladumor/one-signal/license)](LICENSE.md) 11 | 12 | # Give a Star if this package realy usefull to you. it's free :laughing: 13 | 14 | ## :film_strip: here are video tutorials 15 | 16 | #### How to install and how to implement notifications and devices APIs. 17 | 18 | [](https://youtu.be/c1W7unOw3s4) 19 | 20 | #### how to implement Segment and Apps APIs. 21 | 22 | [](https://youtu.be/mxiEZ4H2cgY) 23 | 24 | 25 | 26 | ## Requirements. 27 | 28 | v2.0.0 is released with new features and new APIs with latest version of one signal. this version is supported in 29 | laravel 8 and more. 30 | if you want to use this package in laravel 7 or lower version than you can 31 | use [v1.0.0](https://github.com/shailesh-ladumor/one-signal/blob/master/v1.0.0-guide.md). 32 | 33 | ## Contents 34 | 35 | - [Installation](#installation) 36 | - [Publish the config file](#publish-the-config-file) 37 | - [Add Provider](#add-provider) 38 | - [Add Facade](#add-facade) 39 | - [Usage](#usage) 40 | - [Messages](#messages) 41 | - [Send Notification](#send-notification) 42 | - [push](#push) 43 | - [email](#email) 44 | - [sms](#sms) 45 | - [Customise Contents](#customise-contents) 46 | - [Get Single Notification](#get-single-notification) 47 | - [Get All Notifications](#get-all-notifications) 48 | - [Cancel Notification](#cancel-notification) 49 | - [Notification History](#notification-history) 50 | - [Live Activities](#live-activities) 51 | - [Start Live Activity](#start-live-activity) 52 | - [Update Live Activity](#update-live-activity) 53 | - [User](#user) 54 | - [Create User](#create-user) 55 | - [View User](#view-user) 56 | - [Update User](#update-user) 57 | - [Delete User](#delete-user) 58 | - [View User Identity](#view-user-identity) 59 | - [View User Identity By Sponsorship](#view-user-identity-by-sponsorship) 60 | - [Create Alias](#create-alias) 61 | - [Create Alias by subscription](#create-alias-by-subscription) 62 | - [Delete Alias](#delete-alias) 63 | - [Subscription](#subscription) 64 | - [Create Subscription](#create-subscription) 65 | - [Update Subscription](#update-subscription) 66 | - [Delete Subscription](#delete-subscription) 67 | - [Transfer Subscription](#transfer-subscription) 68 | - [Unsubscribe Notification](#unsubscribe-notification) 69 | - [Template](#template) 70 | - [Create Template](#create-template) 71 | - [Update Template](#update-template) 72 | - [View Template](#view-template) 73 | - [View Templates](#view-templates) 74 | - [Delete Template](#delete-template) 75 | - [Copy Template](#copy-template) 76 | - [Segment](#segment) 77 | - [Create Segment (NEED PAID PLAN)](#create-segment) 78 | - [Delete Segment (NEED PAID PLAN)](#delete-segment) 79 | - [Apps](#apps) 80 | - [View Apps](#view-apps) 81 | - [View App](#view-app) 82 | - [Create App](#create-app) 83 | - [Update App](#update-app) 84 | - [View Outcomes](#view-outcomes) 85 | - [User Device](#user-device) 86 | - [Change Log](#change-log) 87 | - [License](#license) 88 | 89 | ## Watch Other Lavavel tutorial here 90 | 91 | [](https://www.youtube.com/channel/UCuCjzuwBqMqFdh0EU-UwQ-w?sub_confirmation=1)) 92 | 93 | ## Installation 94 | 95 | Install the package by the following command 96 | 97 | composer require ladumor/one-signal:2.0.0 98 | 99 | ## Publish the config file 100 | 101 | Run the following command to publish config file, 102 | 103 | php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider" 104 | 105 | ## Add Provider 106 | 107 | Add the provider to your `config/app.php` into `provider` section if using lower version of laravel, 108 | 109 | Ladumor\OneSignal\OneSignalServiceProvider::class, 110 | 111 | ## Add Facade 112 | 113 | Add the Facade to your `config/app.php` into `aliases` section, 114 | 115 | 'OneSignal' => \Ladumor\OneSignal\OneSignal::class, 116 | 117 | ## Add ENV data 118 | 119 | Add your api keys and OneSignal app id to your `.env`, 120 | 121 | ONE_SIGNAL_APP_ID=XXXXXX-XXXXXX-XXXXXX-XXXXXX (YOUR APP ID) 122 | ONE_SIGNAL_AUTHORIZE=XXXXXX (REST API KEY) 123 | ONE_SIGNAL_AUTH_KEY=XXXXXXX (YOUR USER AUTH KEY) 124 | 125 | You can call them into your code with, 126 | 127 | ## Usage 128 | 129 | ### Send Notification 130 | 131 | #### Push 132 | 133 | For send push notification, use the sendPush method by calling, you can refer 134 | this [docs](https://documentation.onesignal.com/reference/create-message) for more fields details. 135 | 136 | $fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy']; 137 | $message = 'hey!! this is test push.!' 138 | 139 | OneSignal::sendPush($fields, $message); 140 | 141 | Optionally, you can obtain the id of the notification like this, 142 | 143 | $notificationID = OneSignal::sendPush($fields, $message); 144 | echo $notificationID["id"]; 145 | 146 | #### SMS 147 | 148 | For send SMS notification, use the sendSMS method by calling, 149 | 150 | $fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy']; 151 | OR 152 | $fields['include_phone_numbers'] = ['+15558675310']; 153 | 154 | $fields['sms_from']= "+15558675309"; 155 | 156 | $message = 'hey!! this is test SMS.!' 157 | 158 | OneSignal::sendSMS($fields, $message); 159 | 160 | #### EMAIL 161 | 162 | Ensure your Email [setup](https://documentation.onesignal.com/docs/email-setup) is complete. 163 | For send Email notification, use the sendEmail method by calling, see more payload 164 | param [here](https://documentation.onesignal.com/reference/email#schedule-delivery) 165 | 166 | $fields = [ 167 | 'email_from_name' => 'Shailesh', 168 | 'email_from_address' => "sml@gmail.com", 169 | 'email_reply_to_address' => "reply@gmail.com", 170 | 'email_subject' => "Welcome to Cat Facts", 171 | 'email_body' => "html>Welcome to Cat Facts", 172 | 'disable_email_click_tracking' => true, 173 | 'include_unsubscribed' => true 174 | ]; 175 | 176 | $fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy']; 177 | OR 178 | $fields['include_email_tokens'] = ['sample@gmail.com']; 179 | 180 | OneSignal::sendEmail($fields); 181 | 182 | #### Customise Contents 183 | 184 | You can customise a contents and pass it in fields. message does not required when you pass contents 185 | 186 | $fields['include_aliases']['onesignal_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy']; 187 | or 188 | $fields['include_aliases']['external_id'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy']; 189 | 190 | $fields['contents'] = array( 191 | "en" => 'English Message', 192 | "es" => 'Spanish Message', 193 | ); 194 | OneSignal::sendPush($fields); 195 | 196 | ### Get Single Notification 197 | 198 | For retrieve single notification, use the `viewMessage` method with id param by calling, 199 | 200 | OneSignal::viewMessage($notificationId); 201 | 202 | You can check [here](https://documentation.onesignal.com/reference/view-message#response-data) return response format. 203 | 204 | ### Get All Notifications 205 | 206 | For retrieve all notifications, use the `viewMessages` method by calling, 207 | 208 | OneSignal::viewMessages(); 209 | 210 | ### Cancel Notification 211 | 212 | To cancel a notification, use the cancelNotification method by calling, 213 | 214 | $notificationID = 'xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'; 215 | 216 | OneSignal::cancelNotification($notificationID); 217 | 218 | ### Notification History 219 | 220 | For retrieve notification History, use the `getMessageHistory` method by calling, 221 | 222 | $notificationID = 'xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'; 223 | $params = [ 224 | 'events' => 'sent', // required 225 | 'email' => 'sample@gmail.com', // optional, he email address in which to deliver the report. 226 | ]; 227 | 228 | OneSignal::getMessageHistory($notificationID, $params); 229 | 230 | ### Start Live Activity 231 | 232 | This API lets you start a Live Activity by sending a Push Notification., use the `startLiveActivity` method by calling, 233 | See for more params [here](https://documentation.onesignal.com/reference/start-live-activity#body-parameters) 234 | 235 | $params = [ 236 | 'activity_id' => '217aae2b-42ee-4097-bc3f-b7a6e9d15b9b' 237 | ]; 238 | 239 | OneSignal::startLiveActivity($activityType, $params); 240 | 241 | ### Update Live Activity 242 | 243 | Make updates and terminate running Live Activities, use the `updateLiveActivity` method by calling, 244 | See for more params [here](https://documentation.onesignal.com/reference/update-live-activity-api#body-parameters) 245 | 246 | ```injectablephp 247 | $params = [ 248 | 'event' => 'update' 249 | ]; 250 | 251 | OneSignal::updateLiveActivity($params); 252 | ``` 253 | 254 | ### Create User 255 | 256 | For register a new user in OneSignal’s system, use the `createUser` method by calling, 257 | 258 | ```injectablephp 259 | 260 | $params = [ 261 | 'properties' => [ 262 | "tags" => [ 263 | "foo" => "bar", 264 | "this" => "that" 265 | ], 266 | "language" => "en", 267 | "timezone_id" => "America/Los_Angeles", 268 | "lat" => 37.7749, 269 | "long" => -122.4194, 270 | "country" => "US", 271 | "first_active" => 1589788800, 272 | "last_active" => 1589788800, 273 | "purchases" => 0, 274 | "ip" => 3232235777 275 | ], 276 | 'identity' = [ 277 | "external_id" => "An ID, defined by you, to refer to this user in the future", 278 | "facebook_id" => "user_facebook_id", 279 | "amplitude_id" => "user_amplitude_id", 280 | "mixpanel_id" => "user_mixpanel_id", 281 | "custom_alias_N" => "An alternative ID you'll want for retrieving this user's profile data and tags" 282 | ], 283 | 'subscriptions' => [ 284 | [ 285 | "type" => "Email", 286 | "token" => "sample@email.com", 287 | "enabled" => true, 288 | ], 289 | [ 290 | "type" => "SMS", 291 | "token" => "phone_number_in_E.164_format", 292 | "enabled" => true, 293 | ], 294 | [ 295 | "type"=> "iOSPush", 296 | "token"=> "20bdb8fb3bdadc1bef037eefcaeb56ad6e57f3241c99e734062b6ee829271b71", 297 | "enabled"=> true, 298 | "notification_types"=> 1, 299 | "session_time"=> 98, 300 | "session_count"=> 6, 301 | "sdk"=> "", 302 | "device_model"=> "iPhone 14", 303 | "device_os"=> "18.0", 304 | "rooted"=> false, 305 | "test_type"=> 1, 306 | "app_version"=> "5.1.7", 307 | "web_auth"=> "", 308 | "web_p256"=> : "" 309 | ] 310 | ]; 311 | 312 | OneSignal::createUser($params); 313 | ``` 314 | 315 | ### View User 316 | 317 | For retrieve a user including aliases, properties, and subscriptions, use the `viewUser` method by calling, 318 | 319 | ```injectablephp 320 | OneSignal::viewUser($onesignalId, $aliasId); 321 | ``` 322 | 323 | ### Update User 324 | 325 | For update a user’s properties, tags, and subscriptions, use the `updateUser` method by calling, 326 | You can refer [here](https://documentation.onesignal.com/reference/update-user#body-parameters) for more detail about $params 327 | 328 | ```injectablephp 329 | OneSignal::updateUser($onesignalId, $aliasId, $params); 330 | ``` 331 | 332 | ### Delete User 333 | 334 | Delete a user including all associated properties, subscriptions, and identity. use the `deleteUser` method by calling, 335 | 336 | ```injectablephp 337 | OneSignal::deleteUser($onesignalId, $aliasId); 338 | ``` 339 | 340 | ### View User Identity 341 | 342 | Retrieve a user’s identity, use the `viewUserIdentity` method by calling, 343 | 344 | ```injectablephp 345 | OneSignal::viewUserIdentity($onesignalId, $aliasId); 346 | ``` 347 | 348 | ### View User Identity By Sponsorship 349 | 350 | Retrieve a user’s identity by subscription, use the `viewUserIdentityBySubscription` method by calling, 351 | 352 | ```injectablephp 353 | OneSignal::viewUserIdentityBySubscription($subscriptionId); 354 | ``` 355 | 356 | ### Create Alias 357 | 358 | Create an alias for a user, use the `createAlias` method by calling, 359 | Refer docs [here](https://documentation.onesignal.com/reference/create-alias#how-to-use-this-api) for more details 360 | 361 | ```injectablephp 362 | $params = [ 363 | 'external_id' => "", 364 | 'onesignal_id' => "", 365 | ]; 366 | 367 | OneSignal::createAlias($onesignalId, $aliasId, $params); 368 | ``` 369 | 370 | ### Create Alias by subscription 371 | 372 | Create an alias for a user by subscription, use the `createAliasBySubscription` method by calling, 373 | Refer docs [here](https://documentation.onesignal.com/reference/create-alias-by-subscription#how-to-use-this-api) for 374 | more details 375 | 376 | ```injectablephp 377 | $params = [ 378 | 'external_id' => "", 379 | 'onesignal_id' => "", 380 | ]; 381 | 382 | OneSignal::createAliasBySubscription($subscriptionId, $params); 383 | ``` 384 | 385 | ### Delete Alias 386 | 387 | Delete an alias for a user, use the `deleteAlias` method by calling, 388 | Refer docs [here](https://documentation.onesignal.com/reference/delete-alias) for more details 389 | 390 | ```injectablephp 391 | OneSignal::deleteAlias($onesignalId, $aliasId, $aliasLabelToDelete); 392 | ``` 393 | 394 | ### Create Subscription 395 | 396 | Add a new subscription to your user. use the `createSubscription` method by calling, 397 | Refer docs [here](https://documentation.onesignal.com/reference/create-subscription#examples) for more body params 398 | 399 | ```injectablephp 400 | $fields = [ 401 | 'subscription' => [], 402 | ]; 403 | 404 | OneSignal::createSubscription($fields); 405 | ``` 406 | 407 | ### Update Subscription 408 | 409 | Update an existing subscription, use the `updateSubscription` method by calling, 410 | Refer docs [here](https://documentation.onesignal.com/reference/update-subscription) for more details 411 | 412 | ```injectablephp 413 | $fields = [ 414 | 'subscription' => [], 415 | ]; 416 | 417 | OneSignal::updateSubscription($subscriptionId, $fields); 418 | ``` 419 | 420 | ### Delete Subscription 421 | 422 | Delete an existing subscription, use the `deleteSubscription` method by calling, 423 | Refer docs [here](https://documentation.onesignal.com/reference/delete-subscription) for more details 424 | 425 | ```injectablephp 426 | OneSignal::deleteSubscription($subscriptionId); 427 | ``` 428 | 429 | ### Transfer Subscription 430 | 431 | Transfer a subscription to a different user, use the `transferSubscription` method by calling, 432 | Refer docs [here](https://documentation.onesignal.com/reference/transfer-subscription) for more details 433 | 434 | ```injectablephp 435 | $fields = [ 436 | 'identity' => [], 437 | ]; 438 | 439 | OneSignal::transferSubscription($subscriptionId, $fields); 440 | ``` 441 | 442 | ### Unsubscribe Notification 443 | 444 | Unsubscribe a user from a notification, use the `unsubscribeNotification` method by calling, 445 | Refer docs [here](https://documentation.onesignal.com/reference/unsubscribe-with-token) for more details 446 | 447 | ```injectablephp 448 | OneSignal::unsubscribeNotification($notificationId, $token); 449 | ``` 450 | 451 | ### Create Template 452 | 453 | Create a template for push, email and sms, use the `createTemplate` method by calling, 454 | Refer docs [here](https://documentation.onesignal.com/reference/create-template) for push, email and sms template 455 | 456 | ```injectablephp 457 | $fields = [ 458 | 'name' => "", 459 | 'content' => [ 460 | "en": "English Message" 461 | ], 462 | "sms_from"=>"+1234567890", 463 | "isSMS"=> true, 464 | ]; 465 | 466 | OneSignal::createTemplate($fields); 467 | ``` 468 | 469 | ### Update Template 470 | 471 | update a template for push, email and sms, use the `updateTemplate` method by calling, 472 | Refer docs [here](https://documentation.onesignal.com/reference/update-template#examples) for push, email and sms 473 | template 474 | 475 | ```injectablephp 476 | $fields = [ 477 | 'name' => "", 478 | 'content' => [ 479 | "en": "English Message" 480 | ], 481 | "sms_from"=>"+1234567890", 482 | "isSMS"=> true, 483 | ]; 484 | 485 | OneSignal::updateTemplate($templateId, $fields); 486 | ``` 487 | 488 | ### View Template 489 | 490 | For retrieve a template, use the `viewTemplate` method by calling, 491 | 492 | ```injectablephp 493 | OneSignal::viewTemplate($templateId); 494 | ``` 495 | 496 | ### View Templates 497 | 498 | For retrieve all templates, use the `viewTemplates` method by calling, 499 | 500 | ```injectablephp 501 | $params = [ 502 | 'limit' => 10, 503 | 'offset' => 0, 504 | 'channel' => 'push', 505 | ]; 506 | 507 | OneSignal::viewTemplates($params); 508 | ``` 509 | 510 | ### Delete Template 511 | 512 | For delete a template, use the `deleteTemplate` method by calling, 513 | 514 | ```injectablephp 515 | OneSignal::deleteTemplate($templateId); 516 | ``` 517 | 518 | ### Copy Template 519 | 520 | For transfer a template, use the `copyTemplate` method by calling, 521 | 522 | ```injectablephp 523 | $fields = [ 524 | 'target_app_id' => "", 525 | ]; 526 | 527 | OneSignal::copyTemplate($templateId, $fields); 528 | ``` 529 | 530 | ### Create Segment 531 | 532 | For add a new segment in your application, use the `createSegment` method by calling, 533 | 534 | $fields = [ 535 | 'name' => 'iOS, Android, Web', 536 | "filters" => array("field" => "device_type", "relation" => "=", "value" => "Android"), 537 | ]; 538 | 539 | return OneSignal::createSegment($fields); 540 | 541 | You can check [here](https://documentation.onesignal.com/reference/create-segments#body-parameters) supported parameters and guide. 542 | 543 | OneSignal::deleteSegment('YOUR_SEGMENT_ID') 544 | 545 | ### Delete Segment 546 | 547 | You can check [here](https://documentation.onesignal.com/reference/delete-segments#how-to-use-this-api) for more guide. 548 | 549 | ## Apps 550 | 551 | Note*: `Auth key must be set in one-signal.php` how to 552 | get [auth_key](https://documentation.onesignal.com/docs/keys-and-ids#organization-api-key)? 553 | 554 | ### View Apps 555 | 556 | View the details of all of your current OneSignal apps 557 | 558 | $apps = OneSignal::getApps(); 559 | 560 | You can check [here](https://documentation.onesignal.com/reference/view-apps) api response. 561 | 562 | ### View App 563 | 564 | View the details of single of your current OneSignal app or other app by passing app id. 565 | 566 | // It's return default site which is configured in config. 567 | $app = OneSignal::getApp(); 568 | 569 | // you can specify app id as wel but it's optional 570 | $app = OneSignal::getApp('YOUR_APP_ID'); 571 | 572 | You can check [here](https://documentation.onesignal.com/reference/view-an-app) api response. 573 | 574 | ### Create App 575 | 576 | Creates a new OneSignal app. 577 | 578 | $fields = array( 579 | 'name' => "TestByMe" 580 | ); 581 | 582 | OneSignal::createApp($fields); 583 | 584 | You can check [here](https://documentation.onesignal.com/reference/create-an-app) supported parameters and guide. 585 | 586 | ### Update App 587 | 588 | Update a new OneSignal app. 589 | 590 | $fields = array( 591 | 'name' => "TestByMe" 592 | ); 593 | 594 | OneSignal::updateApp($fields); 595 | // you can pass second param as a appId if you want to update other app.. default take from config. 596 | 597 | You can check [here](https://documentation.onesignal.com/reference/update-an-app) supported parameters and guide. 598 | 599 | ### View Outcomes 600 | 601 | View the details of all the outcomes associated with your app. 602 | 603 | $fields = array( 604 | 'outcome_names' => "os__click.count", 605 | 'outcome_time_range' => '1h', 606 | 'outcome_platform' => 0, 607 | 'outcome_attribution' => 'direct' 608 | ); 609 | 610 | OneSignal::getOutcomes($fields); // with params 611 | OneSignal::getOutcomes(); // without any params 612 | // you can pass params in this method, it's optional. 613 | 614 | You can check [here](https://documentation.onesignal.com/reference/view-outcomes) supported parameters and guide. 615 | 616 | ## User Device 617 | 618 | [](https://youtu.be/wOH1qsQ3SL8) 619 | 620 | You can generate a User Device APIs with just one command, 621 | 622 | ```injectablephp 623 | php artisan one-signal.userDevice:publish 624 | ``` 625 | 626 | this command generate following files, 627 | 628 | * UserDeviceAPIController 629 | * UserDeviceAPIRepository 630 | * UserDevice (model) 631 | * Migration 632 | 633 | Also, do not forget to add following routes in to the `api.php` file. 634 | 635 | ```injectablephp 636 | use App\Http\Controllers\API\UserDeviceAPIController; 637 | ``` 638 | 639 | ```injectablephp 640 | Route::post('user-device/register', [UserDeviceAPIController::class, 'registerDevice']); 641 | Route::get('user-device/{playerId}/update-status', [UserDeviceAPIController::class, 'updateNotificationStatus']); 642 | ``` 643 | 644 | ### Change Log 645 | 646 | Please see [Change Log](CHANGELOG.md) here 647 | 648 | ### License 649 | 650 | The MIT License (MIT). Please see [License](LICENSE.md) File for more information 651 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ladumor/one-signal", 3 | "description": "Laravel Wrapper for OneSignal.", 4 | "type": "library", 5 | "license": "MIT", 6 | "keywords": [ 7 | "laravel", 8 | "php", 9 | "one-signal", 10 | "push", 11 | "notification" 12 | ], 13 | "authors": [ 14 | { 15 | "name": "Shailesh Ladumor", 16 | "email": "shaileshmladumor@gmail.com" 17 | } 18 | ], 19 | "require": { 20 | "php": "^8.0", 21 | "illuminate/support": "^11.1|^10.0|^9.0|^8.0", 22 | "ext-curl": "*", 23 | "ext-json": "*" 24 | }, 25 | "require-dev": { 26 | "phpunit/phpunit": "^9.6", 27 | "mockery/mockery": "^1.0" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Ladumor\\OneSignal\\": "src/" 32 | } 33 | }, 34 | "extra": { 35 | "laravel": { 36 | "providers": [ 37 | "\\Ladumor\\OneSignal\\OneSignalServiceProvider" 38 | ], 39 | "aliases": { 40 | "OneSignal": "Ladumor\\OneSignal\\OneSignal" 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /config/one-signal.php: -------------------------------------------------------------------------------- 1 | env('ONE_SIGNAL_URL', 'https://api.onesignal.com/'), 11 | 12 | /* 13 | |------------------------------------------------------------------------------------------- 14 | | App Id - One Signal have different app id for every app. 15 | | 16 | | Based on App you are using, you can change the App Id here and specify here 17 | |------------------------------------------------------------------------------------------- 18 | | 19 | */ 20 | 'app_id' => env('ONE_SIGNAL_APP_ID'), 21 | 22 | /* 23 | |------------------------------------------------------------------------------------------- 24 | | Authorize - One Signal have different Authorize for every app. 25 | | 26 | | Based on App you are using, you can change the Authorize here and specify here 27 | |------------------------------------------------------------------------------------------- 28 | | 29 | */ 30 | 31 | 'authorize' => env('ONE_SIGNAL_AUTHORIZE'), 32 | 33 | /* 34 | |------------------------------------------------------------------------------------------- 35 | | mutable_content - Always defaults to true and cannot be turned off. Allows tracking of notification receives 36 | | and changing of the notification content in app before it is displayed. 37 | |------------------------------------------------------------------------------------------- 38 | | 39 | */ 40 | 'mutable_content' => env('ONE_SIGNAL_MUTABLE_CONTENT', true), 41 | 42 | /* 43 | |------------------------------------------------------------------------------------------- 44 | | Auth Key - One Signal have Auth key of account. 45 | | 46 | | You can manage apps 47 | |------------------------------------------------------------------------------------------- 48 | | 49 | */ 50 | 'auth_key' => env('ONE_SIGNAL_AUTH_KEY'), 51 | ]; 52 | -------------------------------------------------------------------------------- /src/OneSignal.php: -------------------------------------------------------------------------------- 1 | authorization; 20 | } 21 | 22 | /** 23 | * @param string $key 24 | */ 25 | public function setAuthorization(string $key): void 26 | { 27 | $this->authorization = $key; 28 | } 29 | 30 | // One Signal EndPoint Url 31 | protected string $url; 32 | 33 | /** 34 | * @param string $url 35 | * @param array $params 36 | * 37 | * @return string $url 38 | */ 39 | public function getUrl(string $url, array $params = []): string 40 | { 41 | $url = $this->url . $url; 42 | 43 | if (count($params) > 0) { 44 | $url = $url . '?' . http_build_query($params); 45 | } 46 | 47 | return $url; 48 | } 49 | 50 | /** 51 | * @param string $url 52 | */ 53 | public function setUrl(string $url): void 54 | { 55 | $this->url = $url; 56 | } 57 | 58 | // One Signal App ID 59 | protected string $appId; 60 | 61 | /** 62 | * @return string $appId 63 | */ 64 | public function getAppId(): string 65 | { 66 | return $this->appId; 67 | } 68 | 69 | /** 70 | * @param string $appId 71 | */ 72 | public function setAppId(string $appId): void 73 | { 74 | $this->appId = $appId; 75 | } 76 | 77 | // Default mutable content is enabled 78 | protected string $mutableContent; 79 | 80 | /** 81 | * @param string $mutableContent 82 | */ 83 | public function setMutableContent(string $mutableContent): void 84 | { 85 | $this->mutableContent = $mutableContent; 86 | } 87 | 88 | /** 89 | * @return string $mutableContent 90 | */ 91 | public function getMutableContent(): string 92 | { 93 | return $this->mutableContent; 94 | } 95 | 96 | // One Signal Auth key 97 | protected string $authKey; 98 | 99 | /** 100 | * @param string $authKey 101 | */ 102 | public function setAuthKey(string $authKey): void 103 | { 104 | $this->authKey = trim($authKey); 105 | } 106 | 107 | /** 108 | * @return string $authKey 109 | */ 110 | public function getAuthKey(): string 111 | { 112 | return $this->authKey; 113 | } 114 | 115 | /** 116 | * return headers 117 | * 118 | * @return array 119 | */ 120 | protected function getHeaders(): array 121 | { 122 | return [ 123 | 'Content-Type: application/json; charset=utf-8', 124 | 'X-Requested-With:XMLHttpRequest', 125 | 'Authorization: Basic ' . $this->getAuthorization(), 126 | ]; 127 | } 128 | 129 | /** 130 | * GEt Method 131 | * 132 | * @param string $url 133 | * 134 | * @return array|mixed 135 | */ 136 | public function get(string $url): mixed 137 | { 138 | try { 139 | $curl = curl_init(); 140 | 141 | curl_setopt_array($curl, [ 142 | CURLOPT_URL => $url, 143 | CURLOPT_RETURNTRANSFER => true, 144 | CURLOPT_ENCODING => "", 145 | CURLOPT_MAXREDIRS => 10, 146 | CURLOPT_TIMEOUT => 30, 147 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 148 | CURLOPT_CUSTOMREQUEST => "GET", 149 | CURLOPT_HTTPHEADER => $this->getHeaders(), 150 | ]); 151 | 152 | $response = curl_exec($curl); 153 | $err = curl_error($curl); 154 | curl_close($curl); 155 | if (!empty($err)) { 156 | return json_decode($err, true); 157 | } 158 | 159 | return json_decode($response, true); 160 | } catch (\Exception $exception) { 161 | return [ 162 | 'code' => $exception->getCode(), 163 | 'message' => $exception->getMessage(), 164 | ]; 165 | } 166 | } 167 | 168 | /** 169 | * Post Method 170 | * 171 | * @param string $url 172 | * @param string $fields 173 | * 174 | * @return array|mixed 175 | */ 176 | public function post(string $url, string $fields): mixed 177 | { 178 | try { 179 | $ch = curl_init(); 180 | curl_setopt($ch, CURLOPT_URL, $url); 181 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders()); 182 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 183 | curl_setopt($ch, CURLOPT_HEADER, false); 184 | curl_setopt($ch, CURLOPT_POST, true); 185 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 186 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 187 | $response = curl_exec($ch); 188 | $err = curl_error($ch); 189 | curl_close($ch); 190 | 191 | if (!empty($err)) { // return error 192 | return json_decode($err, true); 193 | } 194 | 195 | return json_decode($response, true); // return success 196 | } catch (\Exception $exception) { 197 | return [ 198 | 'code' => $exception->getCode(), 199 | 'message' => $exception->getMessage(), 200 | ]; 201 | } 202 | } 203 | 204 | /** 205 | * Put Method 206 | * 207 | * @param string $url 208 | * @param string $fields 209 | * 210 | * @return array|mixed 211 | */ 212 | public function put(string $url, string $fields): mixed 213 | { 214 | try { 215 | $ch = curl_init(); 216 | curl_setopt($ch, CURLOPT_URL, $url); 217 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders()); 218 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 219 | curl_setopt($ch, CURLOPT_HEADER, false); 220 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 221 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 222 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 223 | 224 | $response = curl_exec($ch); 225 | curl_close($ch); 226 | 227 | if (!empty($err)) { // return error 228 | return json_decode($err, true); 229 | } 230 | 231 | return json_decode($response, true); // return success 232 | } catch (\Exception $exception) { 233 | return [ 234 | 'code' => $exception->getCode(), 235 | 'message' => $exception->getMessage(), 236 | ]; 237 | } 238 | } 239 | 240 | /** 241 | * Patch Method 242 | * 243 | * @param string $url 244 | * @param string $fields 245 | * 246 | * @return array|mixed 247 | */ 248 | public function patch(string $url, string $fields): mixed 249 | { 250 | try { 251 | $ch = curl_init(); 252 | curl_setopt($ch, CURLOPT_URL, $url); 253 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders()); 254 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 255 | curl_setopt($ch, CURLOPT_HEADER, false); 256 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); 257 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 258 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 259 | 260 | $response = curl_exec($ch); 261 | curl_close($ch); 262 | 263 | if (!empty($err)) { // return error 264 | return json_decode($err, true); 265 | } 266 | 267 | return json_decode($response, true); // return success 268 | } catch (\Exception $exception) { 269 | return [ 270 | 'code' => $exception->getCode(), 271 | 'message' => $exception->getMessage(), 272 | ]; 273 | } 274 | } 275 | 276 | /** 277 | * Delete Method 278 | * 279 | * @param string $url 280 | * 281 | * @return array|mixed 282 | */ 283 | public function delete(string $url): mixed 284 | { 285 | try { 286 | $ch = curl_init(); 287 | curl_setopt($ch, CURLOPT_URL, $url); 288 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders()); 289 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 290 | curl_setopt($ch, CURLOPT_HEADER, false); 291 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 292 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 293 | $response = curl_exec($ch); 294 | $err = curl_error($ch); 295 | curl_close($ch); 296 | 297 | if (!empty($err)) { // return error 298 | return json_decode($err, true); 299 | } 300 | 301 | return json_decode($response, true); // return success 302 | } catch (\Exception $exception) { 303 | return [ 304 | 'code' => $exception->getCode(), 305 | 'message' => $exception->getMessage(), 306 | ]; 307 | } 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /src/OneSignalManager.php: -------------------------------------------------------------------------------- 1 | initConfig(); 29 | } 30 | 31 | /** 32 | * Set up required configuration 33 | */ 34 | protected function initConfig(): void 35 | { 36 | $this->setUrl(config('one-signal.url')); 37 | $this->setAppId(config('one-signal.app_id')); 38 | $this->setAuthorization(config('one-signal.authorize')); 39 | $this->setAuthKey(config('one-signal.auth_key')); 40 | $this->setMutableContent(config('one-signal.mutable_content')); 41 | } 42 | 43 | /** 44 | * Send a Push Notification to user on device 45 | * 46 | * @param array $fields 47 | * @param string $message 48 | * 49 | * @return array|mixed 50 | */ 51 | public function sendPush(array $fields, string $message): mixed 52 | { 53 | $content = [ 54 | "en" => $message, 55 | ]; 56 | 57 | $fields['app_id'] = $this->getAppId(); 58 | $fields['mutable_content'] = $this->getMutableContent(); 59 | 60 | if (empty($fields['contents'])) { 61 | $fields['contents'] = $content; 62 | } 63 | 64 | return $this->post($this->getUrl(NOTIFICATIONS, ['c' => 'push']), json_encode($fields)); 65 | } 66 | 67 | /** 68 | * Send an Email to users 69 | * 70 | * @param array $fields 71 | * 72 | * @return array|mixed 73 | */ 74 | public function sendEmail(array $fields): mixed 75 | { 76 | $fields['app_id'] = $this->getAppId(); 77 | 78 | $this->checkEmptyValidation($fields, 'email_subject'); 79 | $this->checkEmptyValidation($fields, 'email_body'); 80 | 81 | return $this->post($this->getUrl(NOTIFICATIONS, ['c' => 'email']), json_encode($fields)); 82 | } 83 | 84 | /** 85 | * Send an SMS to users 86 | * 87 | * @param array $fields 88 | * @param string $message 89 | * 90 | * @return array|mixed 91 | */ 92 | public function sendSMS(array $fields, string $message = ''): mixed 93 | { 94 | $fields['app_id'] = $this->getAppId(); 95 | $content = [ 96 | "en" => $message, 97 | ]; 98 | 99 | if (empty($fields['contents'])) { 100 | $fields['contents'] = $content; 101 | } 102 | 103 | return $this->post($this->getUrl(NOTIFICATIONS, ['c' => 'sms']), json_encode($fields)); 104 | } 105 | 106 | /** 107 | * GET all notifications/messages of any applications. 108 | * 109 | * @param array $params 110 | * 111 | * @return array|mixed 112 | */ 113 | public function viewMessages(array $params = []): mixed 114 | { 115 | $url = $this->getUrl(NOTIFICATIONS) . '?app_id=' . $this->getAppId(); 116 | 117 | if (count($params) > 0) { 118 | $url = $url . join('&', $params); 119 | } 120 | 121 | return $this->get($url); 122 | } 123 | 124 | /** 125 | * Get Single notification/message 126 | * 127 | * @param string $notificationId 128 | * 129 | * @return object 130 | */ 131 | public function viewMessage(string $notificationId): object 132 | { 133 | $url = $this->getUrl(NOTIFICATIONS) . '/' . $notificationId . "?app_id=" . $this->getAppId(); 134 | 135 | return $this->get($url); 136 | } 137 | 138 | /** 139 | * @param string $notificationId 140 | * @param null $appId 141 | * 142 | * @return array|mixed 143 | */ 144 | public function cancelNotification(string $notificationId, $appId = null): mixed 145 | { 146 | if (empty($appId)) { // take a default if does not specified 147 | $appId = $this->getAppId(); 148 | } 149 | 150 | return $this->delete($this->getUrl(NOTIFICATIONS . '/' . $notificationId . '?app_id=' . $appId)); 151 | } 152 | 153 | /** 154 | * Get Message History 155 | * Query params are optional and supported param email,events 156 | * 157 | * @param string $notificationId - notification id 158 | * @param array $params - query params (optional) 159 | * 160 | * @return object|array|mixed 161 | */ 162 | public function getMessageHistory(string $notificationId, array $params = []): object 163 | { 164 | if (empty($notificationId)) { 165 | throw new InvalidArgumentException('Notification id is required'); 166 | } 167 | 168 | if (empty($params['events'])) { 169 | throw new InvalidArgumentException('events is required. Ex. "sent", "clicked"'); 170 | } 171 | 172 | $partUrl = DIRECTORY_SEPARATOR . $notificationId . "/history"; 173 | 174 | $params['app_id'] = $this->getAppId(); 175 | 176 | $url = $this->getUrl(NOTIFICATIONS . $partUrl, $params); 177 | 178 | return $this->get($url); 179 | } 180 | 181 | /** This API lets you start a Live Activity by sending a Push Notification. 182 | * 183 | * @param string $activityType 184 | * @param array $fields 185 | * 186 | * @return mixed 187 | */ 188 | public function startLiveActivity(string $activityType, array $fields): mixed 189 | { 190 | $this->checkEmptyValidation($fields, 'activity_id'); 191 | $this->checkEmptyValidation($fields, 'name'); 192 | $this->checkEmptyValidation($fields, 'event_attributes'); 193 | $this->checkEmptyValidation($fields, 'event_updates'); 194 | $this->checkEmptyValidation($fields, 'contents'); 195 | $this->checkEmptyValidation($fields, 'headings'); 196 | 197 | $url = $this->getUrl(APPS . '/' . $this->getAppId() . '/activities/activity/' . $activityType); 198 | return $this->post($url, json_encode($fields)); 199 | } 200 | 201 | /** Make updates and terminate running Live Activities. 202 | * 203 | * @param string $activityId 204 | * @param array $fields 205 | * 206 | * @return mixed 207 | */ 208 | public function updateLiveActivity(string $activityId, array $fields): mixed 209 | { 210 | if (empty($fields['event'])) { 211 | throw new InvalidArgumentException('event is required. for example: "end", "update"'); 212 | } 213 | 214 | $url = $this->getUrl(APPS . '/' . $this->getAppId() . '/live_activities/' . $activityId . '/notifications'); 215 | return $this->post($url, json_encode($fields)); 216 | } 217 | 218 | /** 219 | * Create a new user. 220 | * 221 | * @param array $fields 222 | * 223 | * @return mixed 224 | */ 225 | public function createUser(array $fields): mixed 226 | { 227 | $url = $this->getUrl(USERS); 228 | return $this->post($url, json_encode($fields)); 229 | } 230 | 231 | /** 232 | * View user details including aliases, properties, and subscriptions 233 | * 234 | * @param string $aliasLabel The type of alias (e.g. 'external_id', 'onesignal_id') 235 | * @param string $aliasId The value of the alias 236 | * 237 | * @return mixed 238 | * @throws InvalidArgumentException 239 | */ 240 | public function viewUser(string $aliasLabel, string $aliasId): mixed 241 | { 242 | $this->checkEmptyValidation($aliasLabel, 'alias_label'); 243 | $this->checkEmptyValidation($aliasId, 'alias_id'); 244 | 245 | $url = $this->getUrl(APPS . '/' . $this->getAppId() . '/' . USERS_BY . '/' . $aliasLabel . '/' . $aliasId); 246 | return $this->get($url); 247 | } 248 | 249 | /** 250 | * Update an existing user. 251 | * 252 | * @param string $aliasLabel The type of alias (e.g. 'external_id', 'onesignal_id') 253 | * @param string $aliasId The value of the alias 254 | * @param array $fields The fields to update 255 | * 256 | * @return mixed 257 | * @throws InvalidArgumentException 258 | */ 259 | public function updateUser(string $aliasLabel, string $aliasId, array $fields): mixed 260 | { 261 | $this->checkEmptyValidation($aliasLabel, 'alias_label'); 262 | $this->checkEmptyValidation($aliasId, 'alias_id'); 263 | 264 | $url = $this->getUrl(APPS . '/' . $this->getAppId() . '/' . USERS_BY . '/' . $aliasLabel . '/' . $aliasId); 265 | return $this->patch($url, json_encode($fields)); 266 | } 267 | 268 | /** 269 | * Delete a user including all associated properties, subscriptions, and identity. 270 | * 271 | * @param string $aliasLabel The type of alias (e.g. 'external_id', 'onesignal_id') 272 | * @param string $aliasId The value of the alias 273 | * 274 | * @return mixed 275 | * @throws InvalidArgumentException 276 | */ 277 | public function deleteUser(string $aliasLabel, string $aliasId): mixed 278 | { 279 | // Validations 280 | $this->checkEmptyValidation($aliasLabel, 'alias_label'); 281 | $this->checkEmptyValidation($aliasId, 'alias_id'); 282 | 283 | // Prepare URL 284 | $url = $this->getUrl(APPS . '/' . $this->getAppId() . '/' . USERS_BY . '/' . $aliasLabel . '/' . $aliasId); 285 | 286 | // Execute API 287 | return $this->delete($url); 288 | } 289 | 290 | /** 291 | *Retrieve all aliases associated with a user using a known alias. 292 | * 293 | * @param string $aliasLabel The type of alias (e.g. 'external_id', 'onesignal_id') 294 | * @param string $aliasId The value of the alias 295 | * 296 | * @return mixed 297 | * @throws InvalidArgumentException 298 | */ 299 | public function viewUserIdentity(string $aliasLabel, string $aliasId): mixed 300 | { 301 | // Validations 302 | $this->checkEmptyValidation($aliasLabel, 'alias_label'); 303 | $this->checkEmptyValidation($aliasId, 'alias_id'); 304 | 305 | // Prepare URL 306 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . USERS_BY . '/' . $aliasLabel . '/' . $aliasId . '/identity'; 307 | $url = $this->getUrl($basicUrl); 308 | 309 | // Execute API 310 | return $this->get($url); 311 | } 312 | 313 | /** 314 | * Retrieve all aliases associated with a user using a known subscription ID. 315 | * 316 | * @param string $subscriptionId The unique identifier that represents a subscription in our system. 317 | * 318 | * @return mixed 319 | * @throws InvalidArgumentException 320 | */ 321 | public function viewUserIdentityBySubscription(string $subscriptionId): mixed 322 | { 323 | // Validations 324 | $this->checkEmptyValidation($subscriptionId, 'subscriptionId'); 325 | 326 | // Prepare URL 327 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . SUBSCRIPTIONS . '/' . $subscriptionId . '/user/identity'; 328 | $url = $this->getUrl($basicUrl); 329 | 330 | // Execute API 331 | return $this->get($url); 332 | } 333 | 334 | /** 335 | * Create or update an alias for a user using a known alias. 336 | * 337 | * @param string $aliasLabel The type of alias (e.g. 'external_id', 'onesignal_id') 338 | * @param string $aliasId The value of the alias 339 | * @param array $fields The fields to update 340 | * 341 | * @return mixed 342 | * @throws InvalidArgumentException 343 | */ 344 | public function createAlias(string $aliasLabel, string $aliasId, array $fields): mixed 345 | { 346 | // Validations 347 | $this->checkEmptyValidation($aliasLabel, 'alias_label'); 348 | $this->checkEmptyValidation($aliasId, 'alias_id'); 349 | 350 | // Prepare URL 351 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . USERS_BY . '/' . $aliasLabel . '/' . $aliasId . '/identity'; 352 | $url = $this->getUrl($basicUrl); 353 | 354 | // Execute API 355 | return $this->patch($url, json_encode($fields)); 356 | } 357 | 358 | /** Create or update an alias for a user using a known subscription ID. 359 | * 360 | * @param string $subscriptionId 361 | * @param array $fields 362 | * 363 | * @return mixed 364 | */ 365 | public function createAliasBySubscription(string $subscriptionId, array $fields): mixed 366 | { 367 | // Validations 368 | $this->checkEmptyValidation($subscriptionId, 'subscriptionId'); 369 | 370 | // Prepare URL 371 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . SUBSCRIPTIONS . '/' . $subscriptionId . '/user/identity'; 372 | $url = $this->getUrl($basicUrl); 373 | 374 | // Execute API 375 | return $this->patch($url, json_encode($fields)); 376 | } 377 | 378 | /** 379 | * @param string $aliasLabel 380 | * @param string $aliasId 381 | * @param string $aliasLabelToDelete 382 | * 383 | * @return mixed 384 | */ 385 | public function deleteAlias(string $aliasLabel, string $aliasId, string $aliasLabelToDelete): mixed 386 | { 387 | // Validations 388 | $this->checkEmptyValidation($aliasLabel, 'alias_label'); 389 | $this->checkEmptyValidation($aliasId, 'alias_id'); 390 | $this->checkEmptyValidation($aliasLabelToDelete, 'alias_label_to_delete'); 391 | 392 | // Prepare URL 393 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . USERS_BY . '/' . $aliasLabel . '/' . $aliasId . '/identity/' . $aliasLabelToDelete; 394 | $url = $this->getUrl($basicUrl); 395 | 396 | // Execute API 397 | return $this->delete($url); 398 | } 399 | 400 | /** 401 | * Add a new subscription to your user. 402 | * 403 | * @param string $aliasLabel The type of alias (e.g. 'external_id', 'onesignal_id') 404 | * @param string $aliasId The value of the alias 405 | * @param array $fields The fields to update 406 | * 407 | * @return mixed 408 | * @throws InvalidArgumentException 409 | */ 410 | public function createSubscription(string $aliasLabel, string $aliasId, array $fields): mixed 411 | { 412 | // validations 413 | $this->checkEmptyValidation($aliasLabel, 'alias_label'); 414 | $this->checkEmptyValidation($aliasId, 'alias_id'); 415 | $this->checkEmptyValidation($fields, 'subscription'); 416 | 417 | // prepare URL 418 | $basicUrl = APPS . '/' . $this->getAppId(); 419 | $partURL = '/' . USERS_BY . '/' . $aliasLabel . '/' . $aliasId . '/' . SUBSCRIPTIONS; 420 | $url = $this->getUrl($basicUrl . $partURL); 421 | 422 | // Execute API 423 | return $this->post($url, json_encode($fields)); 424 | } 425 | 426 | /** 427 | * Update a subscription's properties. 428 | * 429 | * @param string $subscriptionId 430 | * @param array $fields 431 | * 432 | * @return mixed 433 | */ 434 | public function updateSubscription(string $subscriptionId, array $fields): mixed 435 | { 436 | // validations 437 | $this->checkEmptyValidation($subscriptionId, 'subscriptionId'); 438 | $this->checkEmptyValidation($fields, 'subscription'); 439 | 440 | // prepare URL 441 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . SUBSCRIPTIONS . '/' . $subscriptionId; 442 | $url = $this->getUrl($basicUrl); 443 | 444 | // Execute API 445 | return $this->patch($url, json_encode($fields)); 446 | } 447 | 448 | /** 449 | * Delete an existing subscription. 450 | * 451 | * @param string $subscriptionId 452 | * 453 | * @return mixed 454 | */ 455 | public function deleteSubscription(string $subscriptionId): mixed 456 | { 457 | // validations 458 | $this->checkEmptyValidation($subscriptionId, 'subscriptionId'); 459 | 460 | // prepare URL 461 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . SUBSCRIPTIONS . '/' . $subscriptionId; 462 | $url = $this->getUrl($basicUrl); 463 | 464 | // Execute API 465 | return $this->delete($url); 466 | } 467 | 468 | /** 469 | * Transfer a subscription to a different user. 470 | * 471 | * @param string $subscriptionId 472 | * @param array $fields 473 | * 474 | * @return mixed 475 | */ 476 | public function transferSubscription(string $subscriptionId, array $fields): mixed 477 | { 478 | // validations 479 | $this->checkEmptyValidation($subscriptionId, 'subscriptionId'); 480 | $this->checkEmptyValidation($fields, 'identity'); 481 | 482 | // prepare URL 483 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . SUBSCRIPTIONS . '/' . $subscriptionId . '/owner'; 484 | $url = $this->getUrl($basicUrl); 485 | 486 | // Execute API 487 | return $this->patch($url, json_encode($fields)); 488 | } 489 | 490 | /** 491 | * @param string $notificationId 492 | * @param string $token 493 | * 494 | * @return array|mixed 495 | */ 496 | public function unsubscribeNotification(string $notificationId, string $token): mixed 497 | { 498 | // validations 499 | $this->checkEmptyValidation($notificationId, 'notificationId'); 500 | $this->checkEmptyValidation($token, 'token'); 501 | 502 | $basicUrl = APPS . '/' . $this->getAppId() . '/' . NOTIFICATIONS . '/' . $notificationId . '/unsubscribe'; 503 | $url = $this->getUrl($basicUrl, ['token' => $token]); 504 | return $this->post($url, ''); 505 | } 506 | 507 | /** 508 | * Create templates for push, email and sms visible and usable in the dashboard and API. 509 | * 510 | * @param array $fields 511 | * 512 | * @return mixed 513 | */ 514 | public function createTemplate(array $fields): mixed 515 | { 516 | // validations 517 | $this->checkEmptyValidation($fields, 'name'); 518 | 519 | // set app id if not set in payload 520 | if (empty($fields['app_id'])) { 521 | $fields['app_id'] = $this->getAppId(); 522 | } 523 | 524 | // prepare URL 525 | $url = $this->getUrl(TEMPLATES); 526 | 527 | // Execute API 528 | return $this->post($url, json_encode($fields)); 529 | } 530 | 531 | /** 532 | * Update previously created templates for push, email and sms visible and usable in the dashboard and API. 533 | * 534 | * @param string $templateId 535 | * @param array $fields 536 | * 537 | * @return mixed 538 | */ 539 | public function updateTemplate(string $templateId, array $fields): mixed 540 | { 541 | // validations 542 | $this->checkEmptyValidation($fields, 'name'); 543 | 544 | // prepare URL 545 | $url = $this->getUrl(TEMPLATES . '/' . $templateId, ['app_id' => $this->getAppId()]); 546 | 547 | // Execute API 548 | return $this->patch($url, json_encode($fields)); 549 | } 550 | 551 | /** 552 | * Returns an array of templates from an app. 553 | * 554 | * @param array $params 555 | * 556 | * @return mixed 557 | */ 558 | public function viewTemplates(array $params): mixed 559 | { 560 | if (empty($fields['app_id'])) { 561 | $params['app_id'] = $this->getAppId(); 562 | } 563 | if (empty($fields['limit'])) { 564 | $params['limit'] = 50; 565 | } 566 | if (empty($fields['offset'])) { 567 | $params['offset'] = 0; 568 | } 569 | 570 | // prepare URL 571 | $url = $this->getUrl(TEMPLATES, $params); 572 | 573 | // Execute API 574 | return $this->get($url); 575 | } 576 | 577 | /** 578 | * @param string $templateId 579 | * 580 | * @return mixed 581 | */ 582 | public function viewTemplate(string $templateId): mixed 583 | { 584 | // prepare URL 585 | $url = $this->getUrl(TEMPLATES . '/' . $templateId, ['app_id' => $this->getAppId()]); 586 | 587 | // Execute API 588 | return $this->get($url); 589 | } 590 | 591 | /** 592 | * Delete templates from OneSignal. 593 | * 594 | * @param string $templateId 595 | * @param array $params 596 | * 597 | * @return mixed 598 | */ 599 | public function deleteTemplate(string $templateId, array $params): mixed 600 | { 601 | if (empty($fields['app_id'])) { 602 | $params['app_id'] = $this->getAppId(); 603 | } 604 | 605 | // prepare URL 606 | $url = $this->getUrl(TEMPLATES . '/' . $templateId, $params); 607 | 608 | // Execute API 609 | return $this->delete($url); 610 | } 611 | 612 | /** 613 | * Create a duplicate of a template in another app 614 | * 615 | * @param string $templateId 616 | * @param array $fields 617 | * 618 | * @return mixed 619 | */ 620 | public function copyTemplate(string $templateId, array $fields): mixed 621 | { 622 | // validations 623 | $this->checkEmptyValidation($fields, 'target_app_id'); 624 | 625 | // prepare URL 626 | $params['app_id'] = empty($fields['app_id']) ? $this->getAppId() : $fields['app_id']; 627 | 628 | $url = $this->getUrl(TEMPLATES . '/' . $templateId, $params); 629 | 630 | // Execute API 631 | return $this->post($url, json_encode(['target_app_id' => $fields['target_app_id']])); 632 | } 633 | 634 | /** 635 | * Create Segment 636 | * 637 | * @param $fields 638 | * @param null $appId 639 | * 640 | * @return array|mixed 641 | */ 642 | public function createSegment($fields, $appId = null): mixed 643 | { 644 | if (empty($appId)) { // take a default if does not specified 645 | $appId = $this->getAppId(); 646 | } 647 | 648 | return $this->post($this->getUrl(APPS . '/' . $appId . '/' . SEGMENTS), json_encode($fields)); 649 | } 650 | 651 | /** 652 | * @param string $segmentId 653 | * @param null $appId 654 | * 655 | * @return array|mixed 656 | */ 657 | public function deleteSegment(string $segmentId, $appId = null): mixed 658 | { 659 | if (empty($appId)) { // take a default if does not specified 660 | $appId = $this->getAppId(); 661 | } 662 | 663 | return $this->delete($this->getUrl(APPS . '/' . $appId . '/' . SEGMENTS . '/' . $segmentId)); 664 | } 665 | 666 | /** 667 | * View segments for an app 668 | * 669 | * @param string|null $appId 670 | * 671 | * @return array|mixed 672 | */ 673 | public function viewSegments(int $limit = 0, $offset = 0, string $appId = null): mixed 674 | { 675 | if (empty($appId)) { 676 | $appId = $this->getAppId(); 677 | } 678 | 679 | $url = $this->getUrl(APPS . '/' . $appId . '/' . SEGMENTS); 680 | 681 | // Set query params here 682 | if ($limit > 0) { 683 | $url .= '?limit=' . $limit; 684 | if ($offset > 0) { 685 | $url .= '&offset=' . $offset; 686 | } 687 | } 688 | 689 | return $this->get($url); 690 | } 691 | 692 | /** 693 | * GET all apps of your one signal. 694 | * 695 | * @return array|mixed 696 | */ 697 | public function getApps(): mixed 698 | { 699 | $this->setAuthorization($this->getAuthKey()); 700 | 701 | $url = $this->getUrl(APPS); 702 | 703 | return $this->get($url); 704 | } 705 | 706 | /** 707 | * GET single app of your one signal. 708 | * 709 | * @param string|null $appId 710 | * 711 | * @return array|mixed 712 | */ 713 | public function getApp(string $appId = null): mixed 714 | { 715 | $this->setAuthorization($this->getAuthKey()); 716 | 717 | $url = $this->getUrl(APPS . '/' . $appId); 718 | 719 | return $this->get($url); 720 | } 721 | 722 | /** 723 | * Add new application on your one signal. 724 | * 725 | * @param array $fields 726 | * 727 | * @return array|mixed 728 | */ 729 | public function createApp(array $fields): mixed 730 | { 731 | $this->setAuthorization($this->getAuthKey()); 732 | 733 | return $this->post($this->getUrl(APPS), json_encode($fields)); 734 | } 735 | 736 | /** 737 | * Update existing application on your one signal. 738 | * 739 | * @param array $fields 740 | * @param string|null $appId 741 | * 742 | * @return array|mixed 743 | */ 744 | public function updateApp(array $fields, string $appId = null): mixed 745 | { 746 | $this->setAuthorization($this->getAuthKey()); 747 | 748 | if (empty($appId)) { // take a default if does not specified 749 | $appId = $this->getAppId(); 750 | } 751 | 752 | return $this->put($this->getUrl(APPS . '/' . $appId), json_encode($fields)); 753 | } 754 | 755 | /** 756 | * GET all outcomes of any applications. 757 | * Outcomes are only accessible for around 30 days 758 | * 759 | * @param array $params 760 | * 761 | * @return array|mixed 762 | */ 763 | public function getOutcomes(array $params = []): mixed 764 | { 765 | $url = $this->getUrl(APPS . '/outcomes' . $this->getAppId(), $params); 766 | 767 | return $this->get($url); 768 | } 769 | 770 | /** This method check empty validation and throw exception if empty. 771 | * 772 | * @param $value 773 | * @param string $field 774 | * 775 | * @return void 776 | */ 777 | private function checkEmptyValidation($value, string $field): void 778 | { 779 | if (is_array($value) && empty($value[$field])) { 780 | throw new InvalidArgumentException($field . ' is required'); 781 | } 782 | 783 | if (empty($value)) { 784 | throw new InvalidArgumentException($field . ' is required'); 785 | } 786 | } 787 | } 788 | -------------------------------------------------------------------------------- /src/OneSignalServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 21 | $configPath => config_path('one-signal.php'), 22 | ]); 23 | } 24 | 25 | /** 26 | * Register the application services. 27 | * 28 | * @return void 29 | */ 30 | public function register(): void 31 | { 32 | $this->app->singleton('one-signal', function ($app) { 33 | return new OneSignalManager(); 34 | }); 35 | 36 | $this->app->singleton('one-signal.userDevice:publish', function ($app) { 37 | return new PublishUserDevice(); 38 | }); 39 | 40 | $this->commands([ 41 | 'one-signal.userDevice:publish', 42 | ]); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/commands/PublishUserDevice.php: -------------------------------------------------------------------------------- 1 | composer = app()['composer']; 33 | } 34 | 35 | public function handle(): void 36 | { 37 | $controllerDir = app_path('Http/Controllers/API'); 38 | if (!File::isDirectory($controllerDir)) { 39 | File::makeDirectory($controllerDir); 40 | } 41 | 42 | $controllerTemplate = file_get_contents(__DIR__.'/../stubs/UserDeviceAPIController.stub'); 43 | $this->createFile($controllerDir. DIRECTORY_SEPARATOR, 'UserDeviceAPIController.php', $controllerTemplate); 44 | $this->info('UserDeviceController published.'); 45 | 46 | $modelDir = app_path('Models'); 47 | 48 | if (!File::isDirectory($modelDir)) { 49 | File::makeDirectory($modelDir); 50 | } 51 | 52 | $modelTemplate = file_get_contents(__DIR__.'/../stubs/UserDevice.stub'); 53 | $this->createFile($modelDir. DIRECTORY_SEPARATOR, 'UserDevice.php', $modelTemplate); 54 | $this->info('UserDevice published.'); 55 | 56 | $repoDir = app_path('Repositories'); 57 | 58 | if (!File::isDirectory($repoDir)) { 59 | File::makeDirectory($repoDir); 60 | } 61 | 62 | $repoTemplate = file_get_contents(__DIR__.'/../stubs/UserDeviceRepository.stub'); 63 | $this->createFile($repoDir. DIRECTORY_SEPARATOR, 'UserDeviceRepository.php', $repoTemplate); 64 | $this->info('UserDeviceRepository published.'); 65 | 66 | $fileName = date('Y_m_d_His').'_'.'create_user_devices_table.php'; 67 | 68 | $repoTemplate = file_get_contents(__DIR__.'/../stubs/CreateUserDeviceTable.stub'); 69 | $this->createFile(base_path('database/migrations/'), $fileName, $repoTemplate); 70 | $this->info('UserDevice migration created.'); 71 | 72 | $this->info('Generating autoload files'); 73 | $this->composer->dumpOptimized(); 74 | 75 | if ($this->confirm("\nDo you want to migrate database? [y|N]", false)) { 76 | $this->call('migrate'); 77 | } 78 | 79 | $this->info('Greeting From Shailesh Ladumor!'); 80 | } 81 | 82 | /** 83 | * @param string $path 84 | * @param string $fileName 85 | * @param mixed $contents 86 | * 87 | * @return void 88 | */ 89 | public static function createFile(string $path,string $fileName, mixed $contents): void 90 | { 91 | // create the directory if it does not exist with permission 92 | if (!file_exists($path)) { 93 | mkdir($path, 0755, true); 94 | } 95 | 96 | $path = $path.$fileName; 97 | 98 | file_put_contents($path, $contents); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/stubs/CreateUserDeviceTable.stub: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->unsignedBigInteger('user_id'); 19 | $table->string('device_type'); 20 | $table->string('onesignal_id')->unique(); 21 | $table->string('external_id'); 22 | $table->string('aliases'); 23 | $table->boolean('is_active')->default(true); 24 | $table->timestamps(); 25 | 26 | $table->foreign('user_id') 27 | ->references('id') 28 | ->on('users') 29 | ->onDelete('CASCADE') 30 | ->onUpdate('CASCADE'); 31 | }); 32 | } 33 | 34 | /** 35 | * Reverse the migrations. 36 | * 37 | * @return void 38 | */ 39 | public function down() 40 | { 41 | Schema::dropIfExists('user_devices'); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/stubs/UserDevice.stub: -------------------------------------------------------------------------------- 1 | userDeviceRepo = $userDeviceRepo; 26 | } 27 | 28 | /** 29 | * @param Request $request 30 | * 31 | * @return JsonResponse 32 | */ 33 | public function registerDevice(Request $request): JsonResponse 34 | { 35 | $this->userDeviceRepo->updateOrCreate($request->all()); 36 | 37 | return $this->sendSuccess('The device has been registered successfully.'); 38 | } 39 | 40 | /** 41 | * @param string $oneSignalId 42 | * 43 | * @return JsonResponse 44 | */ 45 | public function updateNotificationStatus(string $oneSignalId):JsonResponse 46 | { 47 | $this->userDeviceRepo->updateStatus($oneSignalId); 48 | 49 | return $this->sendSuccess('The notification status has been updated successfully.'); 50 | } 51 | 52 | /** 53 | * @param $message 54 | * 55 | * @return JsonResponse 56 | */ 57 | private function sendSuccess($message): JsonResponse 58 | { 59 | return Response::json([ 60 | 'success' => true, 61 | 'message' => $message 62 | ], 200); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/stubs/UserDeviceRepository.stub: -------------------------------------------------------------------------------- 1 | $input['os_player_id'], 25 | ], [ 26 | 'user_id' => auth()->user()->id, 27 | 'os_player_id' => $input['os_player_id'], 28 | 'device_type' => $input['device_type'] 29 | ]); 30 | } catch (Exception $e) { 31 | throw new UnprocessableEntityHttpException($e->getMessage()); 32 | } 33 | } 34 | 35 | /** 36 | * @param $playerId 37 | * 38 | * @return bool 39 | */ 40 | public function updateStatus($playerId): bool 41 | { 42 | $userDevice = UserDevice::whereOsPlayerId($playerId)->first(); 43 | $userDevice->update(['is_active' => !$userDevice->is_active]); 44 | 45 | return true; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /v1.0.0-guide.md: -------------------------------------------------------------------------------- 1 | # Laravel One Signal 2 | 3 | Laravel One Signal is Laravel Wrapper for [One Signal](https://onesignal.com). One Signal is a great platform for send a push notification to your users. This package mentions in One Signal's official Document. you can see [here](https://documentation.onesignal.com/docs/other-cms-setup) 4 | 5 | [![Total Downloads](https://poser.pugx.org/ladumor/one-signal/downloads)](https://packagist.org/packages/ladumor/one-signal) 6 | [![Daily Downloads](https://poser.pugx.org/ladumor/one-signal/d/daily)](https://packagist.org/packages/ladumor/one-signal) 7 | [![Monthly Downloads](https://poser.pugx.org/ladumor/one-signal/d/monthly)](https://packagist.org/packages/ladumor/one-signal) 8 | [![License](https://poser.pugx.org/ladumor/one-signal/license)](LICENSE.md) 9 | 10 | # Give a Star if this package realy usefull to you. it's free :laughing: 11 | 12 | ## :film_strip: here are video tutorials 13 | #### How to install and how to implement notifications and devices APIs. 14 | 15 | [](https://youtu.be/c1W7unOw3s4) 16 | 17 | #### how to implement Segment and Apps APIs. 18 | [](https://youtu.be/mxiEZ4H2cgY) 19 | 20 | 21 | ## Contents 22 | 23 | - [Installation](#installation) 24 | - [Publish the config file](#publish-the-config-file) 25 | - [Add Provider](#add-provider) 26 | - [Add Facade](#add-facade) 27 | - [Usage](#usage) 28 | - [Send Push Notification](#send-push-notification) 29 | - [Cancel Notification](#cancel-notification) 30 | - [Customise Contents](#customise-contents) 31 | - [Get All Notifications](#get-all-notifications) 32 | - [Get Single Notification](#get-single-notification) 33 | - [Get Single Device](#get-single-device) 34 | - [Create Segment (NEED PAID PLAN)](#create-segment) 35 | - [Delete Segment (NEED PAID PLAN)](#delete-segment) 36 | - [View Apps](#view-apps) 37 | - [View App](#view-app) 38 | - [Create App](#create-app) 39 | - [Update App](#update-app) 40 | - [View Outcomes](#view-outcomes) 41 | - [Get All Devices](#get-all-devices) 42 | - [Create Device](#get-single-device) 43 | - [Update Device](#update-device) 44 | - [Delete Device](#delete-device) 45 | - [User Device](#user-device) 46 | - [Change Log](#change-log) 47 | - [License](#license) 48 | 49 | ## Watch Other Lavavel tutorial here 50 | [](https://www.youtube.com/channel/UCuCjzuwBqMqFdh0EU-UwQ-w?sub_confirmation=1)) 51 | 52 | ## Installation 53 | 54 | Install the package by the following command, 55 | 56 | composer require ladumor/one-signal:1.0.0 57 | 58 | 59 | ## Publish the config file 60 | 61 | Run the following command to publish config file, 62 | 63 | php artisan vendor:publish --provider="Ladumor\OneSignal\OneSignalServiceProvider" 64 | 65 | 66 | ## Add Provider 67 | 68 | Add the provider to your `config/app.php` into `provider` section if using lower version of laravel, 69 | 70 | Ladumor\OneSignal\OneSignalServiceProvider::class, 71 | 72 | 73 | ## Add Facade 74 | 75 | Add the Facade to your `config/app.php` into `aliases` section, 76 | 77 | 'OneSignal' => \Ladumor\OneSignal\OneSignal::class, 78 | 79 | 80 | ## Add ENV data 81 | 82 | Add your api keys and OneSignal app id to your `.env`, 83 | 84 | ONE_SIGNAL_APP_ID=XXXXXX-XXXXXX-XXXXXX-XXXXXX (YOUR APP ID) 85 | ONE_SIGNAL_AUTHORIZE=XXXXXX (REST API KEY) 86 | ONE_SIGNAL_AUTH_KEY=XXXXXXX (YOUR USER AUTH KEY) 87 | 88 | You can call them into your code with, 89 | 90 | 91 | 92 | ## Usage 93 | 94 | ### Send Push Notification 95 | 96 | For send push notification, use the sendPush method by calling, 97 | 98 | $fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy']; 99 | $message = 'hey!! this is test push.!' 100 | 101 | OneSignal::sendPush($fields, $message); 102 | 103 | Optionally, you can obtain the id of the notification like this, 104 | 105 | $notificationID = OneSignal::sendPush($fields, $message); 106 | echo $notificationID["id"]; 107 | 108 | 109 | ### Cancel Notification 110 | 111 | To cancel a notification, use the cancelNotification method by calling, 112 | 113 | $notificationID = 'xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy'; 114 | 115 | OneSignal::cancelNotification($notificationID); 116 | 117 | 118 | ### Customise Contents 119 | 120 | You can customise a contents and pass it in fields. message does not required when you pass contents 121 | 122 | $fields['include_player_ids'] = ['xxxxxxxx-xxxx-xxx-xxxx-yyyyyyyyy']; 123 | $fields['contents'] = array( 124 | "en" => 'English Message', 125 | "es" => 'Spanish Message', 126 | ); 127 | OneSignal::sendPush($fields); 128 | 129 | 130 | ### Get All Notifications 131 | 132 | For retrieve all notifications, use the `getNotifications` method by calling, 133 | 134 | OneSignal::getNotifications(); 135 | You can check [here](https://documentation.onesignal.com/reference#section-result-format-view-notifications) return response format. 136 | 137 | 138 | ### Get Single Notification 139 | 140 | For retrieve single notification, use the `getNotification` method with id param by calling, 141 | 142 | OneSignal::getNotification($notificationId); 143 | You can check [here](https://documentation.onesignal.com/reference#section-result-format-view-notification) return response format. 144 | 145 | ### Create Segment 146 | #### NOTE: REQUIRED ONE-SIGNAL [PAID PLAN](https://documentation.onesignal.com/docs/paid-plan-benefits) 147 | For add a new segment in your application, use the `createSegment` method by calling, 148 | 149 | $fields = [ 150 | 'name' => 'iOS, Android, Web', 151 | "filters" => array("field" => "device_type", "relation" => "=", "value" => "Android"), 152 | ]; 153 | 154 | return OneSignal::createSegment($fields); 155 | 156 | You can check [here](https://documentation.onesignal.com/reference#create-segments) supported parameters and guide. 157 | 158 | OneSignal::deleteSegment('YOUR_SEGMENT_ID') 159 | 160 | 161 | ### Delete Segment 162 | #### NOTE: REQUIRED ONE-SIGNAL [PAID PLAN](https://documentation.onesignal.com/docs/paid-plan-benefits) 163 | 164 | You can check [here](https://documentation.onesignal.com/reference#delete-segments) for more guide. 165 | ## Apps 166 | Note*: `Auth key must be set in one-signal.php` how to get [auth_key](https://documentation.onesignal.com/docs/accounts-and-keys#section-user-auth-key)? 167 | 168 | 169 | ### View Apps 170 | View the details of all of your current OneSignal apps 171 | 172 | $apps = OneSignal::getApps(); 173 | 174 | You can check [here](https://documentation.onesignal.com/reference#view-apps-apps) api response. 175 | 176 | 177 | ### View App 178 | View the details of single of your current OneSignal app or other app by passing app id. 179 | 180 | // It's return default site which is configured in config. 181 | $app = OneSignal::getApp(); 182 | 183 | // you can specify app id as wel but it's optional 184 | $app = OneSignal::getApp('YOUR_APP_ID'); 185 | 186 | 187 | You can check [here](https://documentation.onesignal.com/reference#view-an-app) api response. 188 | 189 | 190 | ### Create App 191 | Creates a new OneSignal app. 192 | 193 | $fields = array( 194 | 'name' => "TestByMe" 195 | ); 196 | 197 | OneSignal::createApp($fields); 198 | 199 | You can check [here](https://documentation.onesignal.com/reference#create-an-app) supported parameters and guide. 200 | 201 | 202 | ### Update App 203 | Update a new OneSignal app. 204 | 205 | $fields = array( 206 | 'name' => "TestByMe" 207 | ); 208 | 209 | OneSignal::updateApp($fields); 210 | // you can pass second param as a appId if you want to update other app.. default take from config. 211 | 212 | You can check [here](https://documentation.onesignal.com/reference#update-an-app) supported parameters and guide. 213 | 214 | 215 | ### View Outcomes 216 | Update a new OneSignal app. 217 | 218 | $fields = array( 219 | 'outcome_names' => "os__click.count", 220 | 'outcome_time_range' => '1h', 221 | 'outcome_platform' => 0, 222 | 'outcome_attribution' => 'direct' 223 | ); 224 | 225 | OneSignal::getOutcomes($fields); // with params 226 | OneSignal::getOutcomes(); // without any params 227 | // you can pass params in this method, it's optional. 228 | 229 | You can check [here](https://documentation.onesignal.com/reference/view-outcomes) supported parameters and guide. 230 | 231 | 232 | ### Get All Devices 233 | ***This method @deperecated by onesingnal.*** 234 | 235 | For retrieve all user devices, use the `getDevices` method by calling, 236 | 237 | OneSignal::getDevices(); 238 | You can check [here](https://documentation.onesignal.com/reference#view-devices) return response format. 239 | 240 | 241 | ### Get Single Device 242 | ***This method @deperecated by onesingnal.*** 243 | 244 | For retrieve single Devices, use the `getDevice` method with id param by calling, 245 | 246 | OneSignal::getDevice($deviceId); 247 | You can check [here](https://documentation.onesignal.com/reference#view-device) return response format. 248 | 249 | 250 | ### Create Device 251 | ***This method @deperecated by onesingnal.*** 252 | 253 | For add a device in your application, use the `addDevice` method by calling, if you want to create device in different application than you can specify `app_id` in `$fields` array. 254 | 255 | $fields = [ 256 | 'device_type' => 0, 257 | 'identifier' => '7abcd558f29d0b1f048083e2834ad8ea4b3d87d8ad9c088b33c132706ff445f0', 258 | 'timezone' => '-28800', 259 | 'game_version' => '1.1', 260 | 'device_os' => '7.0.4', 261 | 'test_type' => 1, 262 | 'device_model' => "iPhone 8,2", 263 | 'tags' => array("foo" => "bar") 264 | ]; 265 | 266 | return OneSignal::addDevice($fields); 267 | You can check [here](https://documentation.onesignal.com/reference#section-example-code-add-a-device) supported parameters and guide. 268 | 269 | 270 | ### Update Device 271 | ***This method @deperecated by onesingnal.*** 272 | 273 | For update a device in your application, use the `addDevice` method by calling, if you want to update device in different application than you can specify `app_id` in `$fields` array. 274 | 275 | $fields = [ 276 | 'device_type' => 0, 277 | 'identifier' => '7abcd558f29d0b1f048083e2834ad8ea4b3d87d8ad9c088b33c132706ff445f0', 278 | 'timezone' => '-28800', 279 | 'game_version' => '1.1', 280 | 'device_os' => '7.0.4', 281 | 'test_type' => 1, 282 | 'device_model' => "iPhone 8,2", 283 | 'tags' => array("foo" => "bar") 284 | ]; 285 | 286 | $playerId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 287 | 288 | return OneSignal::updateDevice($fields, $playerId); 289 | You can check [here](https://documentation.onesignal.com/reference#section-body-parameters) supported parameters and guide. 290 | 291 | ### Delete Device 292 | ***This method @deperecated by onesingnal.*** 293 | 294 | Delete existing device on your application 295 | 296 | ```injectablephp 297 | OneSignal::deleteDevice($deviceId); 298 | ``` 299 | 300 | 301 | ## User Device 302 | [](https://youtu.be/wOH1qsQ3SL8) 303 | 304 | You can generate a User Device APIs with just one command, 305 | 306 | ```injectablephp 307 | php artisan one-signal.userDevice:publish 308 | ``` 309 | 310 | this command generate following files, 311 | 312 | * UserDeviceAPIController 313 | * UserDeviceAPIRepository 314 | * UserDevice (model) 315 | * Migration 316 | 317 | Also, do not forget to add following routes in to the `api.php` file. 318 | 319 | ```injectablephp 320 | use App\Http\Controllers\API\UserDeviceAPIController; 321 | ``` 322 | 323 | ```injectablephp 324 | Route::post('user-device/register', [UserDeviceAPIController::class, 'registerDevice']); 325 | Route::get('user-device/{playerId}/update-status', [UserDeviceAPIController::class, 'updateNotificationStatus']); 326 | ``` 327 | 328 | ### Change Log 329 | Please see [Change Log](CHANGELOG.md) here 330 | ### License 331 | The MIT License (MIT). Please see [License](LICENSE.md) File for more information 332 | --------------------------------------------------------------------------------