|
| 1 | +# laravel-firebase |
| 2 | +This package makes it easy to send notifications using Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/) (FCM) with Laravel Notification Channel. |
| 3 | + |
| 4 | +## Installation |
| 5 | + |
| 6 | +This package can be installed through Composer. |
| 7 | + |
| 8 | +``` bash |
| 9 | +composer require liliom/laravel-firebase |
| 10 | +``` |
| 11 | + |
| 12 | +If you don't use Laravel 5.5+ you have to add the service provider manually |
| 13 | + |
| 14 | +```php |
| 15 | +// config/app.php |
| 16 | +'providers' => [ |
| 17 | + ... |
| 18 | + Liliom\Firebase\FirebaseServiceProvider::class, |
| 19 | + ... |
| 20 | +]; |
| 21 | +``` |
| 22 | + |
| 23 | +Now add you Firebase API Key in `config/services.php`. |
| 24 | + |
| 25 | +```php |
| 26 | +return [ |
| 27 | + .... |
| 28 | + 'firebase' => [ |
| 29 | + 'key' => '' |
| 30 | + ], |
| 31 | + .... |
| 32 | +]; |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +Les's create a notification using artisan commend: |
| 38 | + |
| 39 | +```bash |
| 40 | +php artisan make:notification FirebaseNotification |
| 41 | +``` |
| 42 | + |
| 43 | +Now you can use `firebase` channel in your `vie()` mothod. |
| 44 | + |
| 45 | +```php |
| 46 | +public function via($notifiable) |
| 47 | +{ |
| 48 | + return ['firebase']; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Add a pubilc method `toFirebase($notifiable)` to your notification class, and return an instance of `FirebaseMessage`: |
| 53 | + |
| 54 | +```php |
| 55 | +public function toFirebase($notifiable) |
| 56 | +{ |
| 57 | + return (new \Liliom\Firebase\FirebaseMessage) |
| 58 | + ->notification([ |
| 59 | + 'title' => 'Notification title', |
| 60 | + 'body' => 'Notification body', |
| 61 | + 'sound' => '', // Optional |
| 62 | + 'icon' => '', // Optional |
| 63 | + 'click_action' => '' // Optional |
| 64 | + ]) |
| 65 | + ->setData([ |
| 66 | + 'param' => 'zxy' // Optional |
| 67 | + ]) |
| 68 | + ->setPriority('high'); // Default is 'normal' |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Available methods: |
| 73 | + |
| 74 | +- `getData`: To Set `data`. |
| 75 | +- `getPriority`: To Set `priority`. |
| 76 | +- `getTimeToLive`: To Set `time_to_live`. |
| 77 | +- `getCollapseKey`: To Set `collapse_key`. |
| 78 | +- `getNotification`: To Set `notification`. |
| 79 | +- `getCondition`: To Set `condition`. |
| 80 | +- `getContentAvailable`: To Set `content_available`. |
| 81 | +- `getMutableContent`: To Set `mutable_content`. |
| 82 | +- `getPackageName`: To Set `restricted_package_name`. |
| 83 | + |
| 84 | +When sending to specific device(s), make sure your notifiable entity has `routeNotificationForFirebase` method defined: |
| 85 | +> **Note:** You can send fo many devices by return an array of tokens. |
| 86 | +
|
| 87 | +```php |
| 88 | +/** |
| 89 | + * Route notifications for Firebase channel. |
| 90 | + * |
| 91 | + * @return string|array |
| 92 | + */ |
| 93 | +public function routeNotificationForFirebase() |
| 94 | +{ |
| 95 | + return $this->device_tokens; |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## License |
| 100 | + |
| 101 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments