From 51ca13de78be501d629a0ab072d055c16e58f43f Mon Sep 17 00:00:00 2001 From: KAZI MAINUDDIN AHMED Date: Sat, 26 Nov 2016 00:27:32 +0530 Subject: [PATCH] Signed-off-by: Kazi Mainuddin Ahmed --- .editorconfig | 15 ++ .styleci.yml | 1 + CHANGELOG.md | 22 ++ CONDUCT.md | 74 ++++++ CONTRIBUTING.md | 25 ++ ISSUE_TEMPLATE.md | 27 +++ LICENSE.md | 21 ++ PULL_REQUEST_TEMPLATE.md | 43 ++++ README.md | 108 +++++++++ composer.json | 51 ++++ src/Config/tzsk-push.php | 78 ++++++ src/Facade/Push.php | 22 ++ src/Factory/PusherFactory.php | 57 +++++ src/Provider/PushServiceProvider.php | 34 +++ src/Pusher.php | 340 +++++++++++++++++++++++++++ 15 files changed, 918 insertions(+) create mode 100644 .editorconfig create mode 100644 .styleci.yml create mode 100644 CHANGELOG.md create mode 100644 CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 ISSUE_TEMPLATE.md create mode 100644 LICENSE.md create mode 100644 PULL_REQUEST_TEMPLATE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Config/tzsk-push.php create mode 100644 src/Facade/Push.php create mode 100644 src/Factory/PusherFactory.php create mode 100644 src/Provider/PushServiceProvider.php create mode 100644 src/Pusher.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..cd8eb86 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..247a09c --- /dev/null +++ b/.styleci.yml @@ -0,0 +1 @@ +preset: psr2 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..558d057 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# Changelog + +All Notable changes to `tzsk/push` will be documented in this file. + +Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. + +## NEXT - 2016-11-25 + +### Added +- Nothing + +### Deprecated +- Nothing + +### Fixed +- Nothing + +### Removed +- Nothing + +### Security +- Nothing diff --git a/CONDUCT.md b/CONDUCT.md new file mode 100644 index 0000000..dbc968b --- /dev/null +++ b/CONDUCT.md @@ -0,0 +1,74 @@ +# Contributor Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at `mailtokmahmed@gmail.com`. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..0af2121 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,25 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests on [Github](https://github.com/tzsk/push). + + +## Pull Requests + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - Check the code style with ``$ composer check-style`` and fix it with ``$ composer fix-style``. + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **Create feature branches** - Don't ask us to pull from your master branch. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **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. + + +**Happy coding**! diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..5b48c57 --- /dev/null +++ b/ISSUE_TEMPLATE.md @@ -0,0 +1,27 @@ + + +## Detailed description + +Provide a detailed description of the change or addition you are proposing. + +Make it clear if the issue is a bug, an enhancement or just a question. + +## Context + +Why is this change important to you? How would you use it? + +How can it benefit other users? + +## Possible implementation + +Not obligatory, but suggest an idea for implementing addition or change. + +## Your environment + +Include as many relevant details about the environment you experienced the bug in and how to reproduce it. + +* Version used (e.g. PHP 5.6, HHVM 3): +* Operating system and version (e.g. Ubuntu 16.04, Windows 7): +* Link to your project: +* ... +* ... diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..a25346f --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2016 Kazi Mainuddin Ahmed + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..86246b3 --- /dev/null +++ b/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,43 @@ + + +## Description + +Describe your changes in detail. + +## Motivation and context + +Why is this change required? What problem does it solve? + +If it fixes an open issue, please link to the issue here (if you write `fixes #num` +or `closes #num`, the issue will be automatically closed when the pull is accepted.) + +## How has this been tested? + +Please describe in detail how you tested your changes. + +Include details of your testing environment, and the tests you ran to +see how your change affects other areas of the code, etc. + +## Screenshots (if appropriate) + +## Types of changes + +What types of changes does your code introduce? Put an `x` in all the boxes that apply: +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to change) + +## Checklist: + +Go over all the following points, and put an `x` in all the boxes that apply. + +Please, please, please, don't send your pull request until all of the boxes are ticked. Once your pull request is created, it will trigger a build on our [continuous integration](http://www.phptherightway.com/#continuous-integration) server to make sure your [tests and code style pass](https://help.github.com/articles/about-required-status-checks/). + +- [ ] I have read the **[CONTRIBUTING](CONTRIBUTING.md)** document. +- [ ] My pull request addresses exactly one patch/feature. +- [ ] I have created a branch for this patch/feature. +- [ ] Each individual commit in the pull request is meaningful. +- [ ] I have added tests to cover my changes. +- [ ] If my change requires a change to the documentation, I have updated it accordingly. + +If you're unsure about any of these, don't hesitate to ask. We're here to help! diff --git a/README.md b/README.md new file mode 100644 index 0000000..538d03e --- /dev/null +++ b/README.md @@ -0,0 +1,108 @@ +# push + +[![Latest Version on Packagist][ico-version]][link-packagist] +[![Software License][ico-license]](LICENSE.md) +[![Build Status][ico-travis]][link-travis] +[![Coverage Status][ico-scrutinizer]][link-scrutinizer] +[![Quality Score][ico-code-quality]][link-code-quality] +[![Total Downloads][ico-downloads]][link-downloads] + +This is a `Laravel 5 Package` for [**Pushwoosh**](https://www.pushwoosh.com) Notification Integration. This package currently supports `IOS & Android` other support is coming soon. +This package is built upon [gomoob/php-pushwoosh](https://github.com/gomoob/php-pushwoosh) package. With feew tweeks and Laravel 5 Compatibility. + +## Install + +Via Composer + +``` bash +$ composer require tzsk/push +``` + +## Configure + +` config/app.php ` +```php +'providers' => [ + ... + Tzsk\Push\Provider\PushServiceProvider::class, + ... +], + +'aliases' => [ + ... + 'Push' => Tzsk\Push\Facade\Push::class, + ... +], +``` + +To publish the Configuration file in `config/push.php` Run: +```bash +php artisan vendor:publish +``` + +## Usage + +``` php +use Tzsk\Push\Facade\Push; +... +$response = Push::send("Message Text", function($push) { + $push->setToken("Device Token"); + # OR... + $push->setTokens(["Device 1", "Device 2"]) + ->setTitle("You have a new notification") # For Android. + ->setBody("Message Text") # To override the Message. Optional. + ->setBadge(1) # Default: 1. + ->setPayload(["type" => "ANYTHING", "data" => [] ]) # Default: [] + ->setIcon("http://path/to/icon.png") # For Android. + ->setSmallIcon("pw_notification.png") # For Android. + ->setBanner("http://path/to/banner.png") # For Android. Optional. + ->setSound("res/sound/file/path") # Default: "default" + ->setPriority(1) # Default: 1 + ->setVibration(1) # Default: 1 + ->setIbc("#ffffff"); # Icon Background Color. Default: '#ffffff' +}); + +if ($response->isOk()) { + # Successfully Sent. +} else { + # Something went wrong. + echo $response->getStatusMessage(); # Get failure message. +} +... +``` + +## Change log + +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. + +## Security + +If you discover any security related issues, please email mailtokmahmed@gmail.com instead of using the issue tracker. + +## Credits + +- [Kazi Mainuddin Ahmed][link-author] +- [All Contributors][link-contributors] + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. + +[ico-version]: https://img.shields.io/packagist/v/tzsk/push.svg?style=flat-square +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square +[ico-travis]: https://img.shields.io/travis/tzsk/push/master.svg?style=flat-square +[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/tzsk/push.svg?style=flat-square +[ico-code-quality]: https://img.shields.io/scrutinizer/g/tzsk/push.svg?style=flat-square +[ico-downloads]: https://img.shields.io/packagist/dt/tzsk/push.svg?style=flat-square + +[link-packagist]: https://packagist.org/packages/tzsk/push +[link-travis]: https://travis-ci.org/tzsk/push +[link-scrutinizer]: https://scrutinizer-ci.com/g/tzsk/push/code-structure +[link-code-quality]: https://scrutinizer-ci.com/g/tzsk/push +[link-downloads]: https://packagist.org/packages/tzsk/push +[link-author]: https://github.com/tzsk +[link-contributors]: ../../contributors \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ba3456a --- /dev/null +++ b/composer.json @@ -0,0 +1,51 @@ +{ + "name": "tzsk/push", + "type": "library", + "description": "Pushwoosh for Laravel", + "keywords": [ + "tzsk", + "push" + ], + "homepage": "https://github.com/tzsk/push", + "license": "MIT", + "authors": [ + { + "name": "Kazi Mainuddin Ahmed", + "email": "mailtokmahmed@gmail.com", + "homepage": "http://www.brandcatmedia.com", + "role": "Sr. Laravel Developer" + } + ], + "require": { + "illuminate/support": "~5.1", + "php" : "~5.6|~7.0", + "gomoob/php-pushwoosh": "^1.7" + }, + "require-dev": { + "phpunit/phpunit" : "~4.0||~5.0", + "squizlabs/php_codesniffer": "^2.3" + }, + "autoload": { + "psr-4": { + "Tzsk\\Push\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Tzsk\\Push\\": "tests" + } + }, + "scripts": { + "test": "phpunit", + "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", + "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" + }, + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "config": { + "sort-packages": true + } +} diff --git a/src/Config/tzsk-push.php b/src/Config/tzsk-push.php new file mode 100644 index 0000000..0d603f3 --- /dev/null +++ b/src/Config/tzsk-push.php @@ -0,0 +1,78 @@ + 'sandbox', + + /** + * Connection options available for Pushwoosh. + * + * Here you can specify as many connection you want. + */ + 'connections' => [ + + /** + * Sandbox App Credentials. + * + * Credentials to use for testing. + */ + 'sandbox' => [ + + /** + * APP KEY you create from Pushwoosh. + */ + 'key' => 'APP-API-KEY', + + /** + * TOKEN you get from Pushwoosh. + */ + 'token' => 'APP-API-TOKEN', + + ], + + /** + * Production App Credentials. + * + * Credentials to use for Production. + */ + 'production' => [ + + 'key' => 'APP-API-KEY', + + 'token' => 'APP-API-TOKEN', + + ], + + ], + + /** + * General Settings. + * + * These are some general settings that you can fill to have consistency of the push. + * You can also override these settings on runtime. + */ + 'settings' => [ + + 'icon' => 'HTTP-ICON-URL', + + 'title' => 'DEFAULT-PUSH-TITLE', + + 'body' => 'DEFAULT-PUSH-BODY', + + 'sound' => 'PUSH-SOUND', + + 'vibration' => 1, + + 'priority' => 1, + + 'ibc' => '#33bdfb', + + ], + +]; \ No newline at end of file diff --git a/src/Facade/Push.php b/src/Facade/Push.php new file mode 100644 index 0000000..a6ae812 --- /dev/null +++ b/src/Facade/Push.php @@ -0,0 +1,22 @@ +getConfig(); + + return $this->getClient($config); + } + + /** + * Get the configuration data. + * + * @throws \InvalidArgumentException + * + * @return array + */ + protected function getConfig() + { + $cnf = config('push'); + $config = $cnf['connections'][$cnf['default']]; + $keys = ['key', 'token']; + + foreach ($keys as $key) { + if (!array_key_exists($key, $config)) { + throw new InvalidArgumentException("Missing configuration key [$key]."); + } + } + + return array_only($config, $keys); + } + + /** + * Get the Pushwoosh client. + * + * @param string[] $auth + * + * @return \Gomoob\Pushwoosh\Client\Pushwoosh + */ + protected function getClient(array $auth) + { + return Pushwoosh::create() + ->setApplication($auth['key']) + ->setAuth($auth['token']); + } +} \ No newline at end of file diff --git a/src/Provider/PushServiceProvider.php b/src/Provider/PushServiceProvider.php new file mode 100644 index 0000000..c96fae2 --- /dev/null +++ b/src/Provider/PushServiceProvider.php @@ -0,0 +1,34 @@ +publishes([ + dirname(__DIR__).'/Config/tzsk-push.php' => config_path('push.php'), + ]); + } + + /** + * Register any package services. + * + * @return void + */ + public function register() + { + $this->app->singleton('tzsk-push', function ($app) { + return new Pusher(new PusherFactory()); + }); + } +} \ No newline at end of file diff --git a/src/Pusher.php b/src/Pusher.php new file mode 100644 index 0000000..2efadb5 --- /dev/null +++ b/src/Pusher.php @@ -0,0 +1,340 @@ +push = $factory->make(); + + $this->icon = config('push.settings.icon'); + $this->title = config('push.settings.title'); + $this->body = config('push.settings.body'); + $this->vibration = config('push.settings.vibration'); + $this->sound = config('push.settings.sound'); + $this->priority = config('push.settings.priority'); + $this->ibc = config('push.settings.ibc'); + } + + /** + * @param mixed $title + * @return $this + */ + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + /** + * @param mixed $body + * @return $this + */ + public function setBody($body) + { + $this->body = $body; + + return $this; + } + + /** + * @param mixed $badge + * @return $this + */ + public function setBadge($badge) + { + $this->badge = $badge; + + return $this; + } + + /** + * @param mixed $payload + * @return $this + */ + public function setPayload($payload) + { + $this->payload = $payload; + + return $this; + } + + /** + * @param mixed $icon + * @return $this + */ + public function setIcon($icon) + { + $this->icon = $icon; + + return $this; + } + + /** + * @param mixed $smallIcon + * @return $this + */ + public function setSmallIcon($smallIcon) + { + $this->smallIcon = $smallIcon; + + return $this; + } + + /** + * @param mixed $banner + * @return $this + */ + public function setBanner($banner) + { + $this->banner = $banner; + + return $this; + } + + /** + * @param string $sound + * @return $this + */ + public function setSound($sound) + { + $this->sound = $sound; + + return $this; + } + + /** + * @param int $priority + * @return $this + */ + public function setPriority($priority) + { + $this->priority = $priority; + + return $this; + } + + /** + * @param int $vibration + * @return $this + */ + public function setVibration($vibration) + { + $this->vibration = $vibration; + + return $this; + } + + /** + * @param string $ibc + * @return $this + */ + public function setIbc($ibc) + { + $this->ibc = $ibc; + + return $this; + } + + /** + * Set Device token. + * + * @param $token + * @return $this + */ + public function setToken($token) { + $tokens = array_merge($this->tokens, [$token]); + $this->tokens = $tokens; + + return $this; + } + + /** + * Set Device token array. + * + * @param array $tokens + * @return $this + */ + public function setTokens(array $tokens) { + $tokens = array_merge($this->tokens, $tokens); + $this->tokens = $tokens; + + return $this; + } + + /** + * Get Android Settings. + * + * @return Android + */ + protected function getAndroidSettings() { + + $android = Android::create() + ->setBadges($this->badge) + ->setIbc($this->ibc) + ->setCustomIcon($this->icon) + ->setHeader($this->title) + ->setIcon($this->smallIcon) + ->setPriority($this->priority) + ->setSound($this->sound) + ->setVibration($this->vibration); + if ($this->banner) { + $android->setBanner($this->banner); + } + + return $android; + } + + /** + * Get IOS Settings. + * + * @return IOS + */ + protected function getIosSettings() { + $ios = IOS::create()->setBadges($this->badge); + + return $ios; + } + + /** + * @param $token + * @return CreateMessageRequest + */ + protected function getPushRequest($token) + { + $notification = Notification::create() + ->setPlatforms([ + Platform::ios(), + Platform::android(), + ]) + ->setDevices($token) + ->setIOS($this->getIosSettings()) + ->setAndroid($this->getAndroidSettings()) + ->setData($this->payload) + ->setContent($this->body); + $request = CreateMessageRequest::create()->addNotification($notification); + + return $request; + } + + /** + * Send Push to tokens. + * + * @return object + */ + public function send($message, $callback) { + $this->body = $message; + + call_user_func($callback, $this); + + $request = $this->getPushRequest($this->tokens); + $response = $this->push->createMessage($request); + + return $response; + } + +}