├── .editorconfig ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Exceptions ├── CouldNotSendNotification.php └── InvalidConfiguration.php ├── OneSignalButton.php ├── OneSignalChannel.php ├── OneSignalMessage.php ├── OneSignalPayloadFactory.php ├── OneSignalServiceProvider.php ├── OneSignalWebButton.php └── Traits ├── Categories ├── AppearanceHelpers.php ├── AttachmentHelpers.php ├── ButtonHelpers.php ├── DeliveryHelpers.php ├── GroupingHelpers.php └── SilentHelpers.php └── Deprecated.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `onesignal` will be documented in this file 4 | 5 | ## 2.3.0 - 2021-01-05 6 | - Add support for PHP 8 7 | 8 | ## 2.2.0 - 2021-01-05 9 | - Add compatibility for Laravel 8 10 | 11 | ## 2.1.0 - 2020-03-05 12 | - Add compatibility for Laravel 7 13 | - Add testing on PHP 7.4 14 | 15 | ## 2.0.0 - 2019-09-17 16 | - Add compatibility for Laravel 6 17 | - Remove testing on old PHP versions (7.0,7.1) and test on 7.3 18 | 19 | ## 2.0.0-RC2 - 2019-02-07 20 | - Update the underlying **berkayk/onesignal-laravel** Package to v1.0.1 ([#60](https://github.com/laravel-notification-channels/onesignal/pull/60)) 21 | 22 | ## 2.0.0-RC1 - 2019-01-03 23 | - [Refactor] Switch to trait based class ordering ([#58](https://github.com/laravel-notification-channels/onesignal/pull/58)) 24 | - [Feature] Allow sending to multiple tags ([#73](https://github.com/laravel-notification-channels/onesignal/pull/73)) 25 | - [Feature] Allow sending with an empty subject ([#56](https://github.com/laravel-notification-channels/onesignal/pull/56)) 26 | - [Feature] Allow more customisation of the notification ([#55](https://github.com/laravel-notification-channels/onesignal/pull/55) & [#58](https://github.com/laravel-notification-channels/onesignal/pull/58)) 27 | - [Feature] Add support for silent messages ([#67](https://github.com/laravel-notification-channels/onesignal/pull/67)) 28 | - [Feature] Add support for setting a template ([#77](https://github.com/laravel-notification-channels/onesignal/pull/77)) 29 | - [Feature] Allow sending to segments based on "included and excluded" ([#72](https://github.com/laravel-notification-channels/onesignal/pull/72)) 30 | - [Feature] Allow sending to external user IDs ([#86](https://github.com/laravel-notification-channels/onesignal/pull/86)) 31 | 32 | ## 1.2.0 - 2018-03-09 33 | - [Feature] Added targetting per Tags ([#48](https://github.com/laravel-notification-channels/onesignal/pull/48)) 34 | - [Refactor] Implement payload Factory ([#50](https://github.com/laravel-notification-channels/onesignal/pull/50)) 35 | 36 | ## 1.1.1 - 2018-02-09 37 | - [Feature] Added return for response after sending the message ([#46](https://github.com/laravel-notification-channels/onesignal/pull/46)) 38 | 39 | ## 1.1.0 - 2018-01-08 40 | - Min. PHP Version is now PHP 7.0 and tests were executed on php 7.2 ([#44](https://github.com/laravel-notification-channels/onesignal/pull/44)) 41 | - [Feature] Added possibility to send a Notification based on the User E-Mail ([#40](https://github.com/laravel-notification-channels/onesignal/pull/40)) 42 | - [Feature] Added method to set any parameter on the message ([23](https://github.com/laravel-notification-channels/onesignal/pull/23)) 43 | - [Feature] Added method to set all image attachments at once ([#42](https://github.com/laravel-notification-channels/onesignal/pull/42)) 44 | 45 | ## 1.0.2 - 2018-01-03 46 | 47 | - Add compatiblity for Laravel 5.5 48 | 49 | ## 1.0.0 - 2016-01-01 50 | 51 | - Initial release 52 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Marcel Pociot 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OneSignal notifications channel for Laravel 5.3+ 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/onesignal.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/onesignal) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![Build Status](https://img.shields.io/travis/laravel-notification-channels/onesignal/master.svg?style=flat-square)](https://travis-ci.org/laravel-notification-channels/onesignal) 6 | [![StyleCI](https://styleci.io/repos/65379321/shield)](https://styleci.io/repos/65379321) 7 | [![SensioLabsInsight](https://img.shields.io/sensiolabs/i/9015691f-130d-4fca-8710-72a010abc684.svg?style=flat-square)](https://insight.sensiolabs.com/projects/9015691f-130d-4fca-8710-72a010abc684) 8 | [![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/onesignal.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/onesignal) 9 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/onesignal/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/onesignal/?branch=master) 10 | [![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/onesignal.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/onesignal) 11 | 12 | This package makes it easy to send [OneSignal notifications](https://documentation.onesignal.com/docs) with Laravel 5.3+. 13 | 14 | ## Contents 15 | 16 | - [Installation](#installation) 17 | - [Setting up your OneSignal account](#setting-up-your-onesignal-account) 18 | - [Usage](#usage) 19 | - [Available Message methods](#all-available-methods) 20 | - [Button usage](#button-usage) 21 | - [WebButton usage](#webbutton-usage) 22 | - [Changelog](#changelog) 23 | - [Testing](#testing) 24 | - [Security](#security) 25 | - [Contributing](#contributing) 26 | - [Credits](#credits) 27 | - [License](#license) 28 | 29 | 30 | ## Installation 31 | 32 | You can install the package via composer: 33 | 34 | ```bash 35 | $ composer require laravel-notification-channels/onesignal 36 | ``` 37 | 38 | If you're installing the package in Laravel 5.4 or lower, you must import the service provider: 39 | 40 | ```php 41 | // config/app.php 42 | 'providers' => [ 43 | ... 44 | NotificationChannels\OneSignal\OneSignalServiceProvider::class, 45 | ], 46 | ``` 47 | 48 | ### Setting up your OneSignal account 49 | 50 | Add your OneSignal App ID and REST API Key to your `config/services.php`: 51 | 52 | ```php 53 | // config/services.php 54 | ... 55 | 'onesignal' => [ 56 | 'app_id' => env('ONESIGNAL_APP_ID'), 57 | 'rest_api_url' => env('ONESIGNAL_REST_API_URL', 'https://api.onesignal.com'), 58 | 'rest_api_key' => env('ONESIGNAL_REST_API_KEY'), 59 | 'guzzle_client_timeout' => env('ONESIGNAL_GUZZLE_CLIENT_TIMEOUT', 0), 60 | ], 61 | ... 62 | ``` 63 | 64 | 65 | ## Usage 66 | 67 | Now you can use the channel in your `via()` method inside the notification: 68 | 69 | ``` php 70 | use NotificationChannels\OneSignal\OneSignalChannel; 71 | use NotificationChannels\OneSignal\OneSignalMessage; 72 | use NotificationChannels\OneSignal\OneSignalWebButton; 73 | use Illuminate\Notifications\Notification; 74 | 75 | class AccountApproved extends Notification 76 | { 77 | public function via($notifiable) 78 | { 79 | return [OneSignalChannel::class]; 80 | } 81 | 82 | public function toOneSignal($notifiable) 83 | { 84 | return OneSignalMessage::create() 85 | ->setSubject("Your {$notifiable->service} account was approved!") 86 | ->setBody("Click here to see details.") 87 | ->setUrl('http://onesignal.com') 88 | ->webButton( 89 | OneSignalWebButton::create('link-1') 90 | ->text('Click here') 91 | ->icon('https://upload.wikimedia.org/wikipedia/commons/4/4f/Laravel_logo.png') 92 | ->url('http://laravel.com') 93 | ); 94 | } 95 | } 96 | ``` 97 | 98 | In order to let your Notification know which OneSignal user(s) you are targeting, add the `routeNotificationForOneSignal` method to your Notifiable model. 99 | 100 | You can either return a single player-id, or if you want to notify multiple player IDs just return an array containing all IDs. 101 | 102 | ```php 103 | public function routeNotificationForOneSignal() 104 | { 105 | return 'ONE_SIGNAL_PLAYER_ID'; 106 | } 107 | ``` 108 | 109 | If you want to send the notification based on the OneSignal "syncHashedEmail" feature just return an array with the index "email". **It isn't possible to use multiple E-Mails on one filter because of a limitation of the OneSignal API.** 110 | 111 | ```php 112 | public function routeNotificationForOneSignal() 113 | { 114 | return ['email' => 'example@example.com']; 115 | } 116 | ``` 117 | 118 | If you want to send the notification based on the OneSignal "Tags" feature just return an array with the index "tags". 119 | 120 | ```php 121 | public function routeNotificationForOneSignal() 122 | { 123 | return ['tags' => ['key' => 'device_uuid', 'relation' => '=', 'value' => '1234567890-abcdefgh-1234567']]; 124 | } 125 | ``` 126 | 127 | If you want to send the notification based on an external user id you set using the `setExternalUserId` feature. This makes it really easy to target users based on their Laravel User Ids. 128 | 129 | ```php 130 | public function routeNotificationForOneSignal() 131 | { 132 | return ['include_external_user_ids' => $this->id]; 133 | } 134 | ``` 135 | 136 | ### All available methods 137 | 138 | - `setSubject('')`: Accepts a string value for the title. 139 | - `setBody('')`: Accepts a string value for the notification body. 140 | - `setIcon('')`: Accepts an url for the icon. 141 | - `setUrl('')`: Accepts an url for the notification click event. 142 | - `webButton(OneSignalWebButton $button)`: Allows you to add action buttons to the notification (Chrome 48+ (web push) only). 143 | - `button(OneSignalButton $button)`: Allows you to add buttons to the notification (Supported by iOS 8.0 and Android 4.1+ devices. Icon only works for Android). 144 | - `setData($key, $value)`: Allows you to set additional data for the message payload. For more information check the [OneSignal documentation](https://documentation.onesignal.com/reference). 145 | - `setParameter($key, $value)`: Allows you to set additional parameters for the message payload that are available for the REST API. For more information check the [OneSignal documentation](https://documentation.onesignal.com/reference). 146 | - `setImageAttachments($imageUrl)`: Allows you to set one Image to all possible Attachments [OneSignal Attachment documentation](https://documentation.onesignal.com/reference#section-attachments). 147 | 148 | ### Button usage 149 | 150 | ```php 151 | OneSignalMessage::create() 152 | ->button( 153 | OneSignalButton::create('id') 154 | ->text('button text') 155 | ->icon('button icon') 156 | ); 157 | ``` 158 | 159 | ### WebButton usage 160 | 161 | ```php 162 | OneSignalMessage::create() 163 | ->webButton( 164 | OneSignalWebButton::create('id') 165 | ->text('button text') 166 | ->icon('button icon') 167 | ->url('button url') 168 | ); 169 | ``` 170 | 171 | ## Changelog 172 | 173 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 174 | 175 | ## Testing 176 | 177 | ```bash 178 | $ composer test 179 | ``` 180 | 181 | ## Security 182 | 183 | If you discover any security related issues, please email m.pociot@gmail.com instead of using the issue tracker. 184 | 185 | ## Contributing 186 | 187 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 188 | 189 | ## Credits 190 | 191 | - [Marcel Pociot](https://github.com/mpociot) 192 | - [Freek Van der Herten](https://github.com/freekmurze) 193 | - [Lukas Kämmerling](https://github.com/LKDevelopment) 194 | - [David Llop](https://github.com/Lloople) 195 | - [All Contributors](../../contributors) 196 | 197 | ## License 198 | 199 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 200 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-notification-channels/onesignal", 3 | "description": "OneSignal Notifications driver", 4 | "homepage": "https://github.com/laravel-notification-channels/onesignal", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Marcel Pociot", 9 | "email": "hello@marcelpociot.com", 10 | "homepage": "http://marcelpociot.com" 11 | }, 12 | { 13 | "name": "Freek Van der Herten", 14 | "email": "freek@spatie.be", 15 | "homepage": "https://spatie.be" 16 | }, 17 | { 18 | "name": "Lukas Kämmerling", 19 | "email": "kontakt@lukas-kaemmerling.de", 20 | "homepage": "https://lukas-kaemmerling.de" 21 | }, 22 | { 23 | "name": "David Llop", 24 | "email": "hello@davidllop.com", 25 | "homepage": "http://davidllop.com" 26 | } 27 | ], 28 | "require": { 29 | "php": "^8.0", 30 | "berkayk/onesignal-laravel": "^2.0.0", 31 | "illuminate/notifications": "5.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 32 | "illuminate/support": "5.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0" 33 | }, 34 | "require-dev": { 35 | "mockery/mockery": "^1.3|^1.4", 36 | "orchestra/testbench": "^4.0|^5.0|^7.0|^8.0|^9.0|^10.0", 37 | "phpunit/phpunit": "^8.0|^9.5.10|^10.0|^11.5.3" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "NotificationChannels\\OneSignal\\": "src" 42 | } 43 | }, 44 | "autoload-dev": { 45 | "psr-4": { 46 | "NotificationChannels\\OneSignal\\Test\\": "tests" 47 | } 48 | }, 49 | "scripts": { 50 | "test": "vendor/bin/phpunit" 51 | }, 52 | "extra": { 53 | "laravel": { 54 | "providers": [ 55 | "NotificationChannels\\OneSignal\\OneSignalServiceProvider" 56 | ] 57 | } 58 | }, 59 | "config": { 60 | "sort-packages": true 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Exceptions/CouldNotSendNotification.php: -------------------------------------------------------------------------------- 1 | getBody()->getContents().'`'); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidConfiguration.php: -------------------------------------------------------------------------------- 1 | id = $id; 31 | } 32 | 33 | /** 34 | * Set the message icon. 35 | * 36 | * @param string $value 37 | * @return $this 38 | */ 39 | public function icon($value) 40 | { 41 | $this->icon = $value; 42 | 43 | return $this; 44 | } 45 | 46 | /** 47 | * Set the message subject. 48 | * 49 | * @param string $value 50 | * @return $this 51 | */ 52 | public function text($value) 53 | { 54 | $this->text = $value; 55 | 56 | return $this; 57 | } 58 | 59 | /** 60 | * @return array 61 | */ 62 | public function toArray() 63 | { 64 | return [ 65 | 'id' => $this->id, 66 | 'text' => $this->text, 67 | 'icon' => $this->icon, 68 | ]; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/OneSignalChannel.php: -------------------------------------------------------------------------------- 1 | oneSignal = $oneSignal; 18 | } 19 | 20 | /** 21 | * Send the given notification. 22 | * 23 | * @param mixed $notifiable 24 | * @param \Illuminate\Notifications\Notification $notification 25 | * @return \Psr\Http\Message\ResponseInterface 26 | * 27 | * @throws \NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification 28 | */ 29 | public function send($notifiable, Notification $notification) 30 | { 31 | if (! $userIds = $notifiable->routeNotificationFor('OneSignal', $notification)) { 32 | return; 33 | } 34 | 35 | /** @var ResponseInterface $response */ 36 | $response = $this->oneSignal->sendNotificationCustom( 37 | $this->payload($notifiable, $notification, $userIds) 38 | ); 39 | 40 | if ($response->getStatusCode() !== 200) { 41 | throw CouldNotSendNotification::serviceRespondedWithAnError($response); 42 | } 43 | 44 | return $response; 45 | } 46 | 47 | /** 48 | * @param mixed $notifiable 49 | * @param \Illuminate\Notifications\Notification $notification 50 | * @param mixed $targeting 51 | * @return array 52 | */ 53 | protected function payload($notifiable, $notification, $userIds) 54 | { 55 | return OneSignalPayloadFactory::make($notifiable, $notification, $userIds); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/OneSignalMessage.php: -------------------------------------------------------------------------------- 1 | setBody($body); 36 | } 37 | 38 | /** 39 | * Set the message body. 40 | * 41 | * @param mixed $value 42 | * @return $this 43 | */ 44 | public function setBody($value) 45 | { 46 | return $this->setParameter('contents', $this->parseValueToArray($value)); 47 | } 48 | 49 | /** 50 | * Set the message subject. 51 | * 52 | * @param mixed $value 53 | * @return $this 54 | */ 55 | public function setSubject($value) 56 | { 57 | return $this->setParameter('headings', $this->parseValueToArray($value)); 58 | } 59 | 60 | /** 61 | * Set the message template_id. 62 | * 63 | * @param string $value 64 | * @return $this 65 | */ 66 | public function setTemplate($value) 67 | { 68 | Arr::forget($this->payload, 'contents'); 69 | 70 | return $this->setParameter('template_id', $value); 71 | } 72 | 73 | /** 74 | * @param mixed $value 75 | * @return array 76 | */ 77 | protected function parseValueToArray($value) 78 | { 79 | return (is_array($value)) ? $value : ['en' => $value]; 80 | } 81 | 82 | /** 83 | * Set additional data. 84 | * 85 | * @param string $key 86 | * @param mixed $value 87 | * @return $this 88 | */ 89 | public function setData(string $key, $value) 90 | { 91 | return $this->setParameter("data.{$key}", $value); 92 | } 93 | 94 | /** 95 | * Set parameters. 96 | * 97 | * @param string $key 98 | * @param mixed $value 99 | * @return $this 100 | */ 101 | public function setParameter(string $key, $value) 102 | { 103 | Arr::set($this->payload, $key, $value); 104 | 105 | return $this; 106 | } 107 | 108 | /** 109 | * Get parameters. 110 | * 111 | * @param string $key 112 | * @param mixed $default 113 | * @return mixed 114 | */ 115 | public function getParameter(string $key, $default = null) 116 | { 117 | return Arr::get($this->payload, $key, $default); 118 | } 119 | 120 | /** 121 | * @return array 122 | */ 123 | public function toArray() 124 | { 125 | return $this->payload; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/OneSignalPayloadFactory.php: -------------------------------------------------------------------------------- 1 | toOneSignal($notifiable)->toArray(); 20 | 21 | if (static::isTargetingEmail($targeting)) { 22 | $payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]); 23 | } elseif (static::isTargetingTags($targeting)) { 24 | $array = $targeting['tags']; 25 | $res = count($array) == count($array, COUNT_RECURSIVE); 26 | if ($res) { 27 | $payload['tags'] = collect([$targeting['tags']]); 28 | } else { 29 | $payload['tags'] = collect($targeting['tags']); 30 | } 31 | } elseif (static::isTargetingIncludedSegments($targeting)) { 32 | $payload['included_segments'] = collect($targeting['included_segments']); 33 | } elseif (static::isTargetingExcludedSegments($targeting)) { 34 | $payload['excluded_segments'] = collect($targeting['excluded_segments']); 35 | } elseif (static::isTargetingExternalUserIds($targeting)) { 36 | $payload['include_external_user_ids'] = collect($targeting['include_external_user_ids']); 37 | } else { 38 | $payload['include_player_ids'] = collect($targeting); 39 | } 40 | 41 | return $payload; 42 | } 43 | 44 | /** 45 | * @param mixed $targeting 46 | * @return bool 47 | */ 48 | protected static function isTargetingIncludedSegments($targeting) 49 | { 50 | return is_array($targeting) && array_key_exists('included_segments', $targeting); 51 | } 52 | 53 | /** 54 | * @param mixed $targeting 55 | * @return bool 56 | */ 57 | protected static function isTargetingExternalUserIds($targeting) 58 | { 59 | return is_array($targeting) && array_key_exists('include_external_user_ids', $targeting); 60 | } 61 | 62 | /** 63 | * @param mixed $targeting 64 | * @return bool 65 | */ 66 | protected static function isTargetingExcludedSegments($targeting) 67 | { 68 | return is_array($targeting) && array_key_exists('excluded_segments', $targeting); 69 | } 70 | 71 | /** 72 | * @param mixed $targeting 73 | * @return bool 74 | */ 75 | protected static function isTargetingEmail($targeting) 76 | { 77 | return is_array($targeting) && array_key_exists('email', $targeting); 78 | } 79 | 80 | /** 81 | * @param mixed $targeting 82 | * @return bool 83 | */ 84 | protected static function isTargetingTags($targeting) 85 | { 86 | return is_array($targeting) && array_key_exists('tags', $targeting); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/OneSignalServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->when(OneSignalChannel::class) 17 | ->needs(OneSignalClient::class) 18 | ->give(function () { 19 | $oneSignalConfig = config('services.onesignal'); 20 | 21 | if (is_null($oneSignalConfig)) { 22 | throw InvalidConfiguration::configurationNotSet(); 23 | } 24 | 25 | return new OneSignalClient( 26 | $oneSignalConfig['app_id'], 27 | $oneSignalConfig['rest_api_key'], 28 | '', 29 | 0, 30 | $oneSignalConfig['rest_api_url'] ?? 'https://api.onesignal.com' 31 | ); 32 | }); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/OneSignalWebButton.php: -------------------------------------------------------------------------------- 1 | id = $id; 34 | } 35 | 36 | /** 37 | * Set the message icon. 38 | * 39 | * @param string $value 40 | * @return $this 41 | */ 42 | public function icon($value) 43 | { 44 | $this->icon = $value; 45 | 46 | return $this; 47 | } 48 | 49 | /** 50 | * Set the message subject. 51 | * 52 | * @param string $value 53 | * @return $this 54 | */ 55 | public function text($value) 56 | { 57 | $this->text = $value; 58 | 59 | return $this; 60 | } 61 | 62 | /** 63 | * Set the message url. 64 | * 65 | * @param string $value 66 | * @return $this 67 | */ 68 | public function url($value) 69 | { 70 | $this->url = $value; 71 | 72 | return $this; 73 | } 74 | 75 | /** 76 | * @return array 77 | */ 78 | public function toArray() 79 | { 80 | return [ 81 | 'id' => $this->id, 82 | 'text' => $this->text, 83 | 'icon' => $this->icon, 84 | 'url' => $this->url, 85 | ]; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Traits/Categories/AppearanceHelpers.php: -------------------------------------------------------------------------------- 1 | setParameter('ios_badgeType', 'Increase') 16 | ->setParameter('ios_badgeCount', $count); 17 | } 18 | 19 | /** 20 | * Set the iOS badge decrement count. 21 | * 22 | * @param int $count 23 | * @return $this 24 | */ 25 | public function decrementIosBadgeCount(int $count = 1) 26 | { 27 | return $this->setParameter('ios_badgeType', 'Increase') 28 | ->setParameter('ios_badgeCount', -1 * $count); 29 | } 30 | 31 | /** 32 | * Set the iOS badge count. 33 | * 34 | * @param int $count 35 | * @return $this 36 | */ 37 | public function setIosBadgeCount(int $count) 38 | { 39 | return $this->setParameter('ios_badgeType', 'SetTo') 40 | ->setParameter('ios_badgeCount', $count); 41 | } 42 | 43 | /** 44 | * Set the iOS Sound. 45 | * 46 | * @param string $soundUrl 47 | * @return $this 48 | */ 49 | public function setIosSound(string $soundUrl) 50 | { 51 | return $this->setParameter('ios_sound', $soundUrl); 52 | } 53 | 54 | /** 55 | * Set the Android Sound. 56 | * 57 | * @param string $soundUrl 58 | * @return $this 59 | */ 60 | public function setAndroidSound(string $soundUrl) 61 | { 62 | return $this->setParameter('android_sound', $soundUrl); 63 | } 64 | 65 | /** 66 | * Set the Windows Sound. 67 | * 68 | * @param string $soundUrl 69 | * @return $this 70 | */ 71 | public function setWindowsSound(string $soundUrl) 72 | { 73 | return $this->setParameter('wp_sound', $soundUrl) 74 | ->setParameter('wp_wns_sound', $soundUrl); 75 | } 76 | 77 | /** 78 | * Set the Sound for all Systems. 79 | * 80 | * @param string $soundUrl 81 | * @return $this 82 | */ 83 | public function setSound(string $soundUrl) 84 | { 85 | return $this->setAndroidSound($soundUrl) 86 | ->setIosSound($soundUrl) 87 | ->setWindowsSound($soundUrl); 88 | } 89 | 90 | /** 91 | * Set the message icon. 92 | * 93 | * @param string $iconPath 94 | * 95 | * @deprecated use setIcon instead 96 | * 97 | * @return $this 98 | */ 99 | public function icon(string $iconPath) 100 | { 101 | return $this->setIcon($iconPath); 102 | } 103 | 104 | /** 105 | * Set the message icon. 106 | * 107 | * @param string $iconPath 108 | * @return $this 109 | */ 110 | public function setIcon(string $iconPath) 111 | { 112 | return $this->setParameter('chrome_web_icon', $iconPath) 113 | ->setParameter('chrome_icon', $iconPath) 114 | ->setParameter('adm_small_icon', $iconPath) 115 | ->setParameter('small_icon', $iconPath); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Traits/Categories/AttachmentHelpers.php: -------------------------------------------------------------------------------- 1 | $imageUrl]; 16 | 17 | return $this->setParameter('ios_attachments', $imageUrl); 18 | } 19 | 20 | /** 21 | * Set the Big Picture Image only for Android. 22 | * 23 | * @param string $imageUrl 24 | * @return $this 25 | */ 26 | public function setAndroidBigPicture(string $imageUrl) 27 | { 28 | return $this->setParameter('big_picture', $imageUrl); 29 | } 30 | 31 | /** 32 | * Set the Big Picture Image only for FireOS (Amazon). 33 | * 34 | * @param string $imageUrl 35 | * @return $this 36 | */ 37 | public function setAmazonBigPicture(string $imageUrl) 38 | { 39 | return $this->setParameter('adm_big_picture', $imageUrl); 40 | } 41 | 42 | /** 43 | * Set the Big Picture Image only for Chrome. 44 | * 45 | * @param string $imageUrl 46 | * @return $this 47 | */ 48 | public function setChromeBigPicture(string $imageUrl) 49 | { 50 | return $this->setParameter('chrome_big_picture', $imageUrl); 51 | } 52 | 53 | /** 54 | * Set the Web Image only for Chrome. 55 | * 56 | * @param string $imageUrl 57 | * @return $this 58 | */ 59 | public function setChromeWebImage(string $imageUrl) 60 | { 61 | return $this->setParameter('chrome_web_image', $imageUrl); 62 | } 63 | 64 | /** 65 | * Set the additional URL for all Platforms. 66 | * 67 | * @param string $url 68 | * @return $this 69 | */ 70 | public function setUrl(string $url) 71 | { 72 | return $this->setParameter('url', $url); 73 | } 74 | 75 | /** 76 | * Set an image to all possible attachment variables. 77 | * 78 | * @param string $imageUrl 79 | * @return $this 80 | */ 81 | public function setImageAttachments(string $imageUrl) 82 | { 83 | return $this->setIosAttachment($imageUrl) 84 | ->setAndroidBigPicture($imageUrl) 85 | ->setAmazonBigPicture($imageUrl) 86 | ->setChromeBigPicture($imageUrl) 87 | ->setChromeWebImage($imageUrl); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Traits/Categories/ButtonHelpers.php: -------------------------------------------------------------------------------- 1 | setParameter('web_buttons', array_merge($this->getParameter('web_buttons', []), [$button->toArray()])); 19 | } 20 | 21 | /** 22 | * Adds more than one web button to the message. 23 | * 24 | * @param array[OnSignalWebButton] $buttons 25 | * @return $this 26 | */ 27 | public function setWebButtons(array $buttons) 28 | { 29 | collect($buttons)->map(function ($button) { 30 | $this->setWebButton($button); 31 | }); 32 | 33 | return $this; 34 | } 35 | 36 | /** 37 | * Add a native button to the message. 38 | * 39 | * @param OneSignalButton $button 40 | * @return $this 41 | */ 42 | public function setButton(OneSignalButton $button) 43 | { 44 | return $this->setParameter('buttons', array_merge($this->getParameter('buttons', []), [$button->toArray()])); 45 | } 46 | 47 | /** 48 | * Adds more than one native button to the message. 49 | * 50 | * @param array $buttons 51 | * @return $this 52 | */ 53 | public function setButtons(array $buttons) 54 | { 55 | collect($buttons)->map(function ($button) { 56 | $this->setButton($button); 57 | }); 58 | 59 | return $this; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Traits/Categories/DeliveryHelpers.php: -------------------------------------------------------------------------------- 1 | setParameter('send_after', $date); 16 | } 17 | 18 | /** 19 | * Set the deplayed option. 20 | * 21 | * @param string $delayedOption 22 | * @return $this 23 | */ 24 | public function setDelayedOption(string $delayedOption) 25 | { 26 | return $this->setParameter('delayed_option', $delayedOption); 27 | } 28 | 29 | /** 30 | * Set the delivery at time of the day. Use with delayed option = timezone. 31 | * 32 | * @param string $timeOfDay 33 | * @return $this 34 | */ 35 | public function setDeliveryTimeOfDay(string $timeOfDay) 36 | { 37 | return $this->setParameter('delivery_time_of_day', $timeOfDay); 38 | } 39 | 40 | /** 41 | * Set the Time to Live in Seconds. 42 | * 43 | * @param int $ttl 44 | * @return $this 45 | */ 46 | public function setTtl(int $ttl) 47 | { 48 | return $this->setParameter('ttl', $ttl); 49 | } 50 | 51 | /** 52 | * Set the Priority. 53 | * 54 | * @param int $priority 55 | * @return $this 56 | */ 57 | public function setPriority(int $priority) 58 | { 59 | return $this->setParameter('priority', $priority); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Traits/Categories/GroupingHelpers.php: -------------------------------------------------------------------------------- 1 | setParameter('android_group', $group) 17 | ->setParameter('android_group_message', $groupMessage); 18 | } 19 | 20 | /** 21 | * Set the Amazon (FireOS) Grouping Parameters. 22 | * 23 | * @param string $group 24 | * @param array $groupMessage 25 | * @return $this 26 | */ 27 | public function setAmazonGroup(string $group, array $groupMessage) 28 | { 29 | return $this->setParameter('adm_group', $group) 30 | ->setParameter('adm_group_message', $groupMessage); 31 | } 32 | 33 | /** 34 | * Set the Grouping Parameters for all available Systems (currently Android and Amazon (FireOs)). 35 | * 36 | * @param string $group 37 | * @param array $groupMessage 38 | * @return $this 39 | */ 40 | public function setGroup(string $group, array $groupMessage) 41 | { 42 | return $this->setAndroidGroup($group, $groupMessage) 43 | ->setAmazonGroup($group, $groupMessage); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Traits/Categories/SilentHelpers.php: -------------------------------------------------------------------------------- 1 | payload, 'contents'); //removes any contents that are set by constructor. 17 | 18 | return $this->setParameter('content_available', 1); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Traits/Deprecated.php: -------------------------------------------------------------------------------- 1 | setBody($value); 22 | } 23 | 24 | /** 25 | * Set the message subject. 26 | * 27 | * @param mixed $value 28 | * 29 | * @deprecated Use setSubject instead 30 | * 31 | * @return $this 32 | */ 33 | public function subject($value) 34 | { 35 | return $this->setParameter('headings', $this->parseValueToArray($value)); 36 | } 37 | 38 | /** 39 | * Set the message url. 40 | * 41 | * @param string $value 42 | * 43 | * @deprecated use setUrl Instead 44 | * 45 | * @return $this 46 | */ 47 | public function url($value) 48 | { 49 | return $this->setUrl($value); 50 | } 51 | 52 | /** 53 | * Add a web button to the message. 54 | * 55 | * @param OneSignalWebButton $button 56 | * 57 | * @deprecated use setWebButton instead 58 | * 59 | * @return $this 60 | */ 61 | public function webButton(OneSignalWebButton $button) 62 | { 63 | return $this->setWebButton($button); 64 | } 65 | 66 | /** 67 | * Adds more than one web button to the message. 68 | * 69 | * @param array[OnSignalWebButton] $buttons 70 | * 71 | * @deprecated use setWebButtons instead 72 | * 73 | * @return $this 74 | */ 75 | public function webButtons(array $buttons) 76 | { 77 | return $this->setWebButtons($buttons); 78 | } 79 | 80 | /** 81 | * Add a native button to the message. 82 | * 83 | * @param OneSignalButton $button 84 | * 85 | * @deprecated use setButton instead 86 | * 87 | * @return $this 88 | */ 89 | public function button(OneSignalButton $button) 90 | { 91 | return $this->setButton($button); 92 | } 93 | 94 | /** 95 | * Adds more than one native button to the message. 96 | * 97 | * @param array $buttons 98 | * 99 | * @deprecated use setButtons instead 100 | * 101 | * @return $this 102 | */ 103 | public function buttons(array $buttons) 104 | { 105 | return $this->setButtons($buttons); 106 | } 107 | } 108 | --------------------------------------------------------------------------------