├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── config └── push-notification.php ├── database └── migrations │ └── 2017_05_24_000000_create_notification_logs_table.php └── src ├── Adapters └── Fcm.php ├── Classes ├── LogNotificationClass.php └── PushNotificationClass.php ├── Facades ├── LogNotification.php └── PushNotification.php ├── Jobs └── PushNotificationJob.php └── PushNotificationServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | node_modules/ 3 | npm-debug.log 4 | 5 | # Laravel 4 specific 6 | bootstrap/compiled.php 7 | app/storage/ 8 | 9 | # Laravel 5 & Lumen specific 10 | public/storage 11 | public/hot 12 | storage/*.key 13 | .env.*.php 14 | .env.php 15 | .env 16 | Homestead.yaml 17 | Homestead.json 18 | 19 | # Rocketeer PHP task runner and deployment package. https://github.com/rocketeers/rocketeer 20 | .rocketeer/ 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 webelightdev 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-push-notification 2 | [![Latest Stable Version](https://poser.pugx.org/webelightdev/laravel-push-notification/v/stable)](https://packagist.org/packages/webelightdev/laravel-push-notification) 3 | [![Latest Unstable Version](https://poser.pugx.org/webelightdev/laravel-push-notification/v/unstable)](https://packagist.org/packages/webelightdev/laravel-push-notification) 4 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/webelightdev/laravel-push-notification/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/webelightdev/laravel-push-notification/?branch=master) 5 | [![Total Downloads](https://poser.pugx.org/webelightdev/laravel-push-notification/downloads)](https://packagist.org/packages/webelightdev/laravel-push-notification) 6 | [![License](https://poser.pugx.org/webelightdev/laravel-push-notification/license)](https://packagist.org/packages/webelightdev/laravel-push-notification) 7 | 8 | Push Notifications using Laravel 9 | ``` 10 | PushNotification::send(['deviceToken1', 'deviceToken2',..], 'Notification Message', 'Action Key'); 11 | ``` 12 | - Param1 [Device Tokens] - Send Notification to single/mutiple devices. 13 | - Param2 [Notification Message] - Notification Message that will display on mobile notification tray. 14 | - Param3 [Action Key] - Action key is the helper to redirect user to any page in Mobile app on notificaiton touch. 15 | 16 | ## Installation 17 | To get the latest version of Laravel Push Notification, run following using `composer`: 18 | ``` 19 | composer require webelightdev/laravel-push-notification dev-master 20 | ``` 21 | Or, you may manually update require block and run `composer update` 22 | ``` 23 | "require": { 24 | "webelightdev/laravel-push-notification": "dev-master" 25 | } 26 | ``` 27 | 28 | Once Laravel Push Notification is installed, You need to register the Service Provider in `config/app.php`, 29 | Add following in `providers` list 30 | ``` 31 | Webelightdev\LaravelPushNotification\PushNotificationServiceProvider::class, 32 | ``` 33 | 34 | `composer dump-autoload` will be required. 35 | 36 | To publish the Config, Migration, Service Provider and Facades 37 | ``` 38 | php artisan vendor:publish 39 | ``` 40 | 41 | Finally, run migration to generate Notification Log table 42 | ``` 43 | php artisan migrate 44 | ``` 45 | 46 | Latest snapshot of Push Notification for one of our client using laravel-push-notification plugin 47 | ![img](http://i.imgur.com/IsKFkWW.jpg) 48 | 49 | ## License 50 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 51 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webelightdev/laravel-push-notification", 3 | "description": "Push Notifications using Laravel", 4 | "require": { 5 | "php": "^7.0" 6 | }, 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Yash Barot", 11 | "email": "yashce04@gmail.com" 12 | } 13 | ], 14 | "autoload": { 15 | "psr-4": { 16 | "Webelightdev\\LaravelPushNotification\\": "src" 17 | } 18 | }, 19 | "minimum-stability": "dev" 20 | } 21 | -------------------------------------------------------------------------------- /config/push-notification.php: -------------------------------------------------------------------------------- 1 | 'fcm', 13 | 14 | /* 15 | * Adapter Class 16 | * 17 | * Here you can add current adapter class path which provides 18 | * cloud messaging service. 19 | */ 20 | 'adapter' => \Webelightdev\LaravelPushNotification\Adapters\Fcm::class, 21 | 22 | /* 23 | * Cloud Messaging Services 24 | * 25 | * Here you may configure as many service "services" as you wish, 26 | * and you may even configure multiple services here. 27 | */ 28 | 'services' => [ 29 | 'fcm' => [ 30 | 'url' => env('FCM_URL', 'https://fcm.googleapis.com/fcm/send'), 31 | 'auth_key' => env('FCM_AUTH_KEY', ''), 32 | ], 33 | ], 34 | 35 | /* 36 | * Notification Logs 37 | * 38 | * Here you may configure log table for logging out Push Notification status. 39 | * If you are planning to change table then same name should be in 40 | * generated migration as well. 41 | */ 42 | 'logs' => [ 43 | 'table' => 'notification_logs', 44 | ], 45 | 46 | 'moduleEnable' => [ 47 | 'notification' => true, 48 | ], 49 | ]; 50 | -------------------------------------------------------------------------------- /database/migrations/2017_05_24_000000_create_notification_logs_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->timestamps(); 19 | $table->longtext('device_token')->nullable(); 20 | $table->longtext('notification_message')->nullable(); 21 | $table->longtext('error_message')->nullable(); 22 | $table->enum('status', ['success', 'failure']); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists('notification_logs'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Adapters/Fcm.php: -------------------------------------------------------------------------------- 1 | url = config('push-notification.services.fcm.url'); 18 | $this->authKey = config('push-notification.services.fcm.auth_key'); 19 | } 20 | 21 | public function pushNotification($deviceTokens, $message, $action, $title = null) 22 | { 23 | foreach ($deviceTokens as $deviceToken) { 24 | $response = $this->callService($deviceToken, $message, $action, $title); 25 | $this->logResponse($response, $deviceToken, $message, $action); 26 | } 27 | } 28 | 29 | public function callService($deviceToken, $message, $action, $title = null) 30 | { 31 | return file_get_contents($this->url, false, $this->setStream($deviceToken, $message, $action, $title)); 32 | } 33 | 34 | public function setStream($deviceToken, $message, $action, $title = null) 35 | { 36 | $postData['to'] = $deviceToken; 37 | $postData['notification']['body'] = $message; 38 | $postData['notification']['title'] = $title; 39 | $postData['data']['body'] = $message; 40 | $postData['data']['title'] = $title; 41 | $postData['data']['click_action'] = $action; 42 | 43 | $streamOptions = [ 44 | 'http' => [ 45 | 'method' => 'POST', 46 | 'header' => "Authorization: key={$this->authKey}\r\n"."Content-Type: application/json\r\n", 47 | 'content' => json_encode($postData), 48 | ], 49 | ]; 50 | 51 | return stream_context_create($streamOptions); 52 | } 53 | 54 | /** 55 | * @param string $data 56 | */ 57 | public function logResponse($data, $deviceToken, $message, $action) 58 | { 59 | $response = json_decode($data); 60 | if ($response->failure == 1) { 61 | LogNotification::error($deviceToken, $message, $response->results[0]->error); 62 | } else { 63 | LogNotification::info($deviceToken, $message); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Classes/LogNotificationClass.php: -------------------------------------------------------------------------------- 1 | logTable = config('push-notification.logs.table'); 17 | } 18 | 19 | public function info($deviceToken, $notification, $errorMessage = null) 20 | { 21 | $this->log($deviceToken, $notification, $errorMessage, 'success'); 22 | } 23 | 24 | public function error($deviceToken, $notification, $errorMessage) 25 | { 26 | $this->log($deviceToken, $notification, $errorMessage, 'failure'); 27 | } 28 | 29 | public function log($deviceToken, $notification, $errorMessage, $status) 30 | { 31 | DB::table($this->logTable)->insert([ 32 | 'device_token' => $deviceToken, 33 | 'notification_message' => $notification, 34 | 'error_message' => $errorMessage, 35 | 'status' => $status, 36 | ]); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Classes/PushNotificationClass.php: -------------------------------------------------------------------------------- 1 | json(['message' => $message]); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Facades/LogNotification.php: -------------------------------------------------------------------------------- 1 | deviceTokens = $deviceTokens; 28 | $this->message = $message; 29 | $this->action = $action; 30 | $this->title = $title; 31 | } 32 | 33 | /** 34 | * Execute the job. 35 | * 36 | * @return void 37 | */ 38 | public function handle() 39 | { 40 | $adapter = config('push-notification.adapter'); 41 | $objAdapter = new $adapter(); 42 | $objAdapter->pushNotification($this->deviceTokens, $this->message, $this->action, $this->title); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/PushNotificationServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([__DIR__.'/../config/push-notification.php' => config_path('push-notification.php')]); 20 | // Migration 21 | $this->publishes([__DIR__.'/../database/migrations' => $this->app->databasePath().'/migrations'], 'migrations'); 22 | } 23 | 24 | /** 25 | * Register the application services. 26 | * 27 | * @return void 28 | */ 29 | public function register() 30 | { 31 | // Push Notification Service 32 | $this->app->bind('push-notification', function () { 33 | return new PushNotificationClass(); 34 | }); 35 | // Log Notification Service 36 | $this->app->bind('log-notification', function () { 37 | return new LogNotificationClass(); 38 | }); 39 | } 40 | } 41 | --------------------------------------------------------------------------------