├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── Exception │ ├── BadRequestException.php │ ├── PermissionDeniedException.php │ └── RequestException.php └── PushProcessor.php └── tests ├── .gitignore ├── TestCase.php ├── Unit └── PushProcessorTest.php └── bootstrap.php /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [**.js] 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [**.less] 17 | indent_style = space 18 | indent_size = 2 19 | 20 | [**.css] 21 | indent_style = space 22 | indent_size = 2 23 | 24 | [**.php] 25 | indent_style = space 26 | indent_size = 4 27 | 28 | [**.html] 29 | indent_style = space 30 | indent_size = 2 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | .idea 4 | build 5 | docs 6 | phpunit.xml 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Vladimir 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ionic push php sdk 2 | 3 | This package may be helpful for sending ionic push notifications 4 | 5 | ## Install 6 | 7 | Via Composer 8 | 9 | ``` bash 10 | $ composer require dmitrovskiy/ionic-push-php 11 | ``` 12 | 13 | ## Usage 14 | 15 | ``` php 16 | $pusher = new Dmitrovskiy\IonicPush\PushProcessor( 17 | 'PROFILE', 18 | 'AUTHORIZATION_TOKEN' 19 | ); 20 | 21 | $devices = array( 22 | //... 23 | ); 24 | 25 | $notification = array( 26 | //... 27 | ); 28 | 29 | $pusher->notify($devices, $notification); 30 | ``` 31 | 32 | ## License 33 | 34 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dmitrovskiy/ionic-push-php", 3 | "description": "ionic push notifications php sdk", 4 | "keywords": [ 5 | "ionic", 6 | "push", 7 | "notifications" 8 | ], 9 | "homepage": "https://github.com/dmitrovskiy/ionic-push-php", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Vladimir Dmitrovskiy", 14 | "email": "dmitrovskiyvl@gmail.com", 15 | "homepage": "https://github.com/dmitrovskiy", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php" : ">=5.3.0", 21 | "guzzlehttp/guzzle" : "^6.1" 22 | }, 23 | "require-dev": { 24 | "phpunit/phpunit" : "4.*" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "Dmitrovskiy\\IonicPush\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "Dmitrovskiy\\IonicPush\\Tests\\": "tests" 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | ./src 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/Exception/BadRequestException.php: -------------------------------------------------------------------------------- 1 | profile = $profile; 39 | $this->token = $token; 40 | $this->ionicPushEndPoint = $ionicPushEndPoint; 41 | } 42 | 43 | public function getProfile() 44 | { 45 | return $this->profile; 46 | } 47 | 48 | public function getToken() 49 | { 50 | return $this->token; 51 | } 52 | 53 | public function getPushEndPoint() 54 | { 55 | return $this->ionicPushEndPoint; 56 | } 57 | 58 | public function setPushEndPoint($ionicPushEndpoint) 59 | { 60 | $this->ionicPushEndPoint = $ionicPushEndpoint; 61 | } 62 | 63 | /** 64 | * @param array $devices 65 | * @param array $notification 66 | * 67 | * @return mixed 68 | */ 69 | public function notify(array $devices, array $notification) 70 | { 71 | $headers = $this->getNotificationHeaders(); 72 | $body = $this->getNotificationBody($devices, $notification); 73 | 74 | return $this->sendRequest($headers, $body); 75 | } 76 | 77 | protected function getNotificationHeaders() 78 | { 79 | $authorization = sprintf("Bearer %s", $this->getToken()); 80 | 81 | return array( 82 | 'Authorization' => $authorization, 83 | 'Content-Type' => 'application/json' 84 | ); 85 | } 86 | 87 | /** 88 | * @param array $devices 89 | * @param array $notification 90 | * 91 | * @return string 92 | */ 93 | protected function getNotificationBody(array $devices, array $notification) 94 | { 95 | $body = array( 96 | 'profile' => $this->getProfile(), 97 | 'tokens' => $devices, 98 | 'notification' => $notification 99 | ); 100 | 101 | return json_encode($body); 102 | } 103 | 104 | /** 105 | * @param $headers 106 | * @param $body 107 | * 108 | * @return mixed|null 109 | * @throws BadRequestException 110 | * @throws PermissionDeniedException 111 | * @throws RequestException 112 | */ 113 | protected function sendRequest($headers, $body) 114 | { 115 | $request = new Request( 116 | 'POST', 117 | $this->ionicPushEndPoint, 118 | $headers, 119 | $body 120 | ); 121 | $client = new Client(); 122 | 123 | try { 124 | $response = $client->send($request); 125 | return $response; 126 | } catch (ClientException $e) { 127 | switch ($e->getCode()) { 128 | case 401: 129 | case 403: { 130 | throw new PermissionDeniedException( 131 | "Permission denied sending push", $e->getCode(), $e 132 | ); 133 | } 134 | case 400: { 135 | throw new BadRequestException( 136 | "Bad request sending push", 400, $e 137 | ); 138 | } 139 | } 140 | } catch (\Exception $e) { 141 | throw new RequestException( 142 | "An error occurred when sending push request with message: {$e->getMessage()}", 143 | $e->getCode(), 144 | $e 145 | ); 146 | } 147 | 148 | return null; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | credentials.php -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | assertNotNull($instance); 16 | } 17 | 18 | public function testGetProfile() 19 | { 20 | $profile = 'profile'; 21 | $instance = new PushProcessor($profile, 'token'); 22 | $this->assertEquals($instance->getProfile(), $profile); 23 | } 24 | 25 | public function testGetAppApiSecret() 26 | { 27 | $token = 'token'; 28 | $instance = new PushProcessor('profile', $token); 29 | $this->assertEquals($instance->getToken(), $token); 30 | } 31 | 32 | public function testGetPushEndPoint() 33 | { 34 | $pushEndPoint = 'testEndPoint'; 35 | $instance = new PushProcessor('profile', 'token', $pushEndPoint); 36 | $this->assertEquals($instance->getPushEndPoint(), $pushEndPoint); 37 | } 38 | 39 | public function testSetPushEndPoint() 40 | { 41 | $newEndPoint = 'newEndPoint'; 42 | $instance = new PushProcessor('profile', 'token'); 43 | $instance->setPushEndPoint($newEndPoint); 44 | 45 | $this->assertEquals($instance->getPushEndPoint(), $newEndPoint); 46 | } 47 | 48 | /** 49 | * @expectedException \Dmitrovskiy\IonicPush\Exception\PermissionDeniedException 50 | */ 51 | public function testNotifyPermissionDenied() 52 | { 53 | $instance = new PushProcessor('profile', 'token'); 54 | $instance->notify(array(), array()); 55 | } 56 | 57 | /** 58 | * @expectedException \Dmitrovskiy\IonicPush\Exception\RequestException 59 | */ 60 | public function testNotifyFailedRequest() 61 | { 62 | $instance = new PushProcessor( 63 | 'profile', 'token', 'wrong http address' 64 | ); 65 | $instance->notify(array(), array()); 66 | } 67 | 68 | public function testNotifySuccess(){ 69 | $credentials = require __DIR__. '/../credentials.php'; 70 | $instance = new PushProcessor( 71 | $credentials['profile'], $credentials['token'] 72 | ); 73 | /** @var ResponseInterface $response */ 74 | $response = $instance->notify($credentials['device_tokens'],[ 75 | 'message' => 'Hello World!!' 76 | ]); 77 | $this->assertEquals(201,$response->getStatusCode()); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |