├── tests └── .gitkeep ├── .gitignore ├── .travis.yml ├── src ├── Davibennun │ └── LaravelPushNotification │ │ ├── Facades │ │ └── PushNotification.php │ │ ├── LaravelPushNotificationServiceProvider.php │ │ ├── App.php │ │ └── PushNotification.php └── config │ └── config.php ├── phpunit.xml ├── composer.json └── README.md /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | # whitelist 4 | branches: 5 | only: 6 | - master 7 | - laravel5 8 | 9 | php: 10 | - 5.6 11 | - 7.0 12 | - 7.1 13 | - 7.2 14 | 15 | before_script: 16 | - curl -s http://getcomposer.org/installer | php 17 | - php composer.phar install --dev 18 | 19 | script: phpunit 20 | -------------------------------------------------------------------------------- /src/Davibennun/LaravelPushNotification/Facades/PushNotification.php: -------------------------------------------------------------------------------- 1 | array( 6 | 'environment' =>'development', 7 | 'certificate' =>'/path/to/certificate.pem', 8 | 'passPhrase' =>'password', 9 | 'service' =>'apns' 10 | ), 11 | 'appNameAndroid' => array( 12 | 'environment' =>'production', 13 | 'apiKey' =>'yourAPIKey', 14 | 'service' =>'gcm' 15 | ) 16 | 17 | ); -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "turbo124/laravel-push-notification", 3 | "description": "Laravel package to send push notifications to mobile devices (apns, gcm)", 4 | "keywords": ["apns","gcm","push","notification", "laravel"], 5 | "authors": [ 6 | { 7 | "name": "DaviBenNun", 8 | "email": "davi@andradenunes.org" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=5.3.0", 13 | "illuminate/support": "4.*|^6.0|^7.0|^8.0", 14 | "sly/notification-pusher": "dev-master" 15 | }, 16 | "autoload": { 17 | "psr-0": { 18 | "Davibennun\\LaravelPushNotification": "src/" 19 | } 20 | }, 21 | "minimum-stability": "dev", 22 | "repositories": [ 23 | { 24 | "type": "vcs", 25 | "url": "https://github.com/turbo124/NotificationPusher" 26 | } 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /src/Davibennun/LaravelPushNotification/LaravelPushNotificationServiceProvider.php: -------------------------------------------------------------------------------- 1 | package('davibennun/laravel-push-notification'); 23 | } 24 | 25 | /** 26 | * Register the service provider. 27 | * 28 | * @return void 29 | */ 30 | public function register() 31 | { 32 | $this->app->singleton('pushNotification', function ($app) { 33 | return new PushNotification(); 34 | }); 35 | } 36 | 37 | /** 38 | * Get the services provided by the provider. 39 | * 40 | * @return array 41 | */ 42 | public function provides() 43 | { 44 | return array(); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /src/Davibennun/LaravelPushNotification/App.php: -------------------------------------------------------------------------------- 1 | pushManager = new PushManager($config['environment'] == "development" ? PushManager::ENVIRONMENT_DEV : PushManager::ENVIRONMENT_PROD); 13 | 14 | $adapterClassName = 'Sly\\NotificationPusher\\Adapter\\'.ucfirst($config['service']); 15 | 16 | $adapterConfig = $config; 17 | unset($adapterConfig['environment'], $adapterConfig['service']); 18 | 19 | $this->adapter = new $adapterClassName($adapterConfig); 20 | } 21 | 22 | public function to($addressee) 23 | { 24 | $this->addressee = is_string($addressee) ? new Device($addressee) : $addressee; 25 | 26 | return $this; 27 | } 28 | 29 | public function send($message, $options = array()) { 30 | $push = new Push($this->adapter, $this->addressee, ($message instanceof Message) ? $message : new Message($message, $options)); 31 | 32 | $this->pushManager->add($push); 33 | 34 | $this->pushManager->push(); 35 | 36 | return $this; 37 | } 38 | 39 | public function getFeedback() { 40 | return $this->pushManager->getFeedback($this->adapter); 41 | } 42 | } -------------------------------------------------------------------------------- /src/Davibennun/LaravelPushNotification/PushNotification.php: -------------------------------------------------------------------------------- 1 | newInstanceArgs(func_get_args()); 15 | } 16 | 17 | public function Device() 18 | { 19 | $instance = (new \ReflectionClass('Sly\NotificationPusher\Model\Device')); 20 | return $instance->newInstanceArgs(func_get_args()); 21 | } 22 | 23 | public function DeviceCollection() 24 | { 25 | $instance = (new \ReflectionClass('Sly\NotificationPusher\Collection\DeviceCollection')); 26 | return $instance->newInstanceArgs(func_get_args()); 27 | } 28 | 29 | public function PushManager() 30 | { 31 | $instance = (new \ReflectionClass('Sly\NotificationPusher\PushManager')); 32 | return $instance->newInstanceArgs(func_get_args()); 33 | } 34 | 35 | public function ApnsAdapter() 36 | { 37 | $instance = (new \ReflectionClass('Sly\NotificationPusher\Adapter\ApnsAdapter')); 38 | return $instance->newInstanceArgs(func_get_args()); 39 | } 40 | 41 | public function GcmAdapter() 42 | { 43 | $instance = (new \ReflectionClass('Sly\NotificationPusher\Model\GcmAdapter')); 44 | return $instance->newInstanceArgs(func_get_args()); 45 | } 46 | 47 | public function Push() 48 | { 49 | $instance = (new \ReflectionClass('Sly\NotificationPusher\Model\Push')); 50 | return $instance->newInstanceArgs(func_get_args()); 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel Push Notification 2 | ========= 3 | 4 | Package to enable sending push notifications to devices 5 | 6 | Installation 7 | ---- 8 | 9 | Update your `composer.json` file to include this package as a dependency 10 | 11 | Laravel 5 & Lumen 12 | 13 | ```json 14 | "turbo124/laravel-push-notification": "dev-laravel5" 15 | ``` 16 | Laravel 4.* 17 | ```json 18 | "turbo124/laravel-push-notification": "dev-master" 19 | ``` 20 | 21 | Register the PushNotification service provider by adding it to the providers array. 22 | ```php 23 | 'providers' => array( 24 | ... 25 | 'Davibennun\LaravelPushNotification\LaravelPushNotificationServiceProvider' 26 | ) 27 | ``` 28 | 29 | Alias the PushNotification facade by adding it to the aliases array in the `app/config/app.php` file. 30 | ```php 31 | 'aliases' => array( 32 | ... 33 | 'PushNotification' => 'Davibennun\LaravelPushNotification\Facades\PushNotification' 34 | ) 35 | ``` 36 | 37 | # Configuration 38 | 39 | Copy the config file into your project by running: (Lumen users skip this) 40 | 41 | Laravel 5 42 | ```php 43 | php artisan vendor:publish --provider="Davibennun\LaravelPushNotification\LaravelPushNotificationServiceProvider" --tag="config" 44 | ``` 45 | 46 | Laravel 4.* 47 | ``` 48 | php artisan config:publish davibennun/laravel-push-notification 49 | ``` 50 | 51 | This will generate a config file like this 52 | ```php 53 | array( 54 | 'appNameIOS'=>array( 55 | 'environment' => 'development', 56 | 'certificate' => '/path/to/certificate.pem', 57 | 'passPhrase' => 'password', 58 | 'service' => 'apns' 59 | ), 60 | 'appNameAndroid'=>array( 61 | 'environment' => 'production', 62 | 'apiKey' => 'yourAPIKey', 63 | 'service' => 'gcm' 64 | ) 65 | ); 66 | ``` 67 | Where all first level keys corresponds to an service configuration, each service has its own properties, android for instance have `apiKey` and IOS uses `certificate` and `passPhrase`. You can set as many services configurations as you want, one for each app. 68 | 69 | ##### Dont forget to set `service` key to identify IOS `'service'=>'apns'` and Android `'service'=>'gcm'` 70 | 71 | ##### The certificate path must be an absolute path, so in the configuration file you can use these: 72 | ``` 73 | //Path to the 'app' folder 74 | 'certificate'=>app_path().'/myCert.pem' 75 | ``` 76 | Laravel functions are also available `public_path()` `storage_path()` `base_path()` 77 | 78 | # Usage 79 | ```php 80 | 81 | PushNotification::app('appNameIOS') 82 | ->to($deviceToken) 83 | ->send('Hello World, i`m a push message'); 84 | 85 | ``` 86 | Where app argument `appNameIOS` refers to defined service in config file. 87 | 88 | ###Dynamic configuration and Lumen users 89 | You can set the app config array directly: (keep in mind the array schema) 90 | ```php 91 | //iOS app 92 | PushNotification::app(['environment' => 'development', 93 | 'certificate' => '/path/to/certificate.pem', 94 | 'passPhrase' => 'password', 95 | 'service' => 'apns']); 96 | //Android app 97 | PushNotification::app(['environment' => 'production', 98 | 'apiKey' => 'yourAPIKey', 99 | 'service' => 'gcm']); 100 | 101 | ``` 102 | 103 | 104 | To multiple devices and optioned message: 105 | ```php 106 | $devices = PushNotification::DeviceCollection(array( 107 | PushNotification::Device('token', array('badge' => 5)), 108 | PushNotification::Device('token1', array('badge' => 1)), 109 | PushNotification::Device('token2') 110 | )); 111 | $message = PushNotification::Message('Message Text',array( 112 | 'badge' => 1, 113 | 'sound' => 'example.aiff', 114 | 115 | 'actionLocKey' => 'Action button title!', 116 | 'locKey' => 'localized key', 117 | 'locArgs' => array( 118 | 'localized args', 119 | 'localized args', 120 | ), 121 | 'launchImage' => 'image.jpg', 122 | 123 | 'custom' => array('custom data' => array( 124 | 'we' => 'want', 'send to app' 125 | )) 126 | )); 127 | 128 | $collection = PushNotification::app('appNameIOS') 129 | ->to($devices) 130 | ->send($message); 131 | 132 | // get response for each device push 133 | foreach ($collection->pushManager as $push) { 134 | $response = $push->getAdapter()->getResponse(); 135 | } 136 | 137 | // access to adapter for advanced settings 138 | $push = PushNotification::app('appNameAndroid'); 139 | $push->adapter->setAdapterParameters(['sslverifypeer' => false]); 140 | ``` 141 | This package is wrapps [Notification Package] and adds some flavor to it. 142 | 143 | #### Usage advice 144 | This package should be used with [Laravel Queues], so pushes dont blocks the user and are processed in the background, meaning a better flow. 145 | 146 | 147 | 148 | [Notification Package]:https://github.com/Ph3nol/NotificationPusher 149 | [Laravel Queues]:http://laravel.com/docs/queues 150 | --------------------------------------------------------------------------------