From 697153b063b4439f13f96b1e990b1fb1060ec28e Mon Sep 17 00:00:00 2001 From: Ben Wilkins Date: Tue, 6 Dec 2016 10:28:56 -0600 Subject: [PATCH] Initial commit --- README.md | 110 ++++++++++++++++++++++++ composer.json | 19 ++++ src/FcmChannel.php | 65 ++++++++++++++ src/FcmMessage.php | 101 ++++++++++++++++++++++ src/FcmNotificationServiceProvider.php | 36 ++++++++ src/config/laravel-fcm-notification.php | 8 ++ 6 files changed, 339 insertions(+) create mode 100644 composer.json create mode 100755 src/FcmChannel.php create mode 100755 src/FcmMessage.php create mode 100755 src/FcmNotificationServiceProvider.php create mode 100644 src/config/laravel-fcm-notification.php diff --git a/README.md b/README.md index a023dbe..5b569c8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,112 @@ # laravel-fcm-notification Laravel FCM (Firebase Cloud Messaging) Notification Channel + +[![Latest Version](https://img.shields.io/github/release/benwilkins/laravel-analyst.svg?style=flat-square)](https://github.com/benwilkins/laravel-analyst/releases) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) + +Use this package to send push notifications via Laravel to Firebase Cloud Messaging. Laravel 5.3+ required. + +## Install + +This package can be installed through Composer. + +``` bash +composer require benwilkins/laravel-fcm-notification:@dev-master +``` + +Once installed, add the service provider: + +```php +// config/app.php +'providers' => [ + ... + Benwilkins\FCM\FcmNotificationServiceProvider::class, + ... +]; +``` + +Publish the config file: + +``` bash +php artisan vendor:publish --provider="Benwilkins\FCM\FcmNotificationServiceProvider" +``` + +The following config file will be published in `config/laravel-fcm-notification.php`. Add your Firebase API Key here. + +```php +return [ + /* + * Add the Firebase API key + */ + 'api_key' => '' +]; +``` + +## Example Usage + +Use Artisan to create a notification: + +```bash +php artisan make:notification SomeNotification +``` + +Return `[fcm]` in the `public function via($notifiable)` method of your notification: + +```php +public function via($notifiable) +{ + return ['fcm']; +} +``` + +Add the method `public function toFcm($notifiable)` to your notification, and return an instance of `FcmMessage`: + +```php +public function toFcm($notifiable) +{ + $message = new Benwilkins\FcmMessage(); + $message->content([ + 'title' => 'Foo', + 'body' => 'Bar', + 'sound' => '', // Optional + 'icon' => '', // Optional + 'click_action' => '' // Optional + ])->data([ + 'param1' => 'baz' // Optional + ])->priority(Benwilkins\FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'. + + return $message; +} +``` + +When sending to specific device, make sure your notifiable entity has `routeNotificationForFcm` method defined: + +```php +/** + * Route notifications for the FCM channel. + * + * @return string + */ +public function routeNotificationForFcm() +{ + return $this->device_token; +} +``` + +When sending to a topic, you may define so within the `toFcm` method in the notification: + +```php +public function toFcm($notifiable) +{ + $message = new Benwilkins\FcmMessage(); + $message->to('the-topic', $recipientIsTopic = true) + ->content([...]) + ->data([...]); + + return $message; +} +``` + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..199cd58 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "benwilkins/laravel-fcm-notification", + "description": "Laravel FCM (Firebase Cloud Messaging) Notification Channel", + "authors": [ + { + "name": "Ben Wilkins", + "email": "bentwilkins@gmail.com" + } + ], + "minimum-stability": "dev", + "require": { + "guzzlehttp/guzzle": "^6.2" + }, + "autoload": { + "psr-4": { + "Benwilkins\\FCM\\": "src/" + } + } +} diff --git a/src/FcmChannel.php b/src/FcmChannel.php new file mode 100755 index 0000000..64bb800 --- /dev/null +++ b/src/FcmChannel.php @@ -0,0 +1,65 @@ +client = $client; + } + + /** + * @param mixed $notifiable + * @param Notification $notification + */ + public function send($notifiable, Notification $notification) + { + /** @var FcmMessage $message */ + $message = $notification->toFCM($notifiable); + + if (is_null($message->getTo())) { + if (!$to = $notifiable->routeNotificationFor('fcm')) { + return; + } + + $message->to($to); + } + + $this->client->post(self::API_URI, [ + 'headers' => [ + 'Authorization' => 'key=' . $this->getApiKey(), + 'Content-Type' => 'application/json', + ], + 'body' => $message->formatData(), + ]); + } + + /** + * @return string + */ + private function getApiKey() + { + return config('laravel-fcm-notification.api_key'); + } +} \ No newline at end of file diff --git a/src/FcmMessage.php b/src/FcmMessage.php new file mode 100755 index 0000000..5205368 --- /dev/null +++ b/src/FcmMessage.php @@ -0,0 +1,101 @@ +to = '/topics/' . $recipient; + } else { + $this->to = $recipient; + } + + return $this; + } + + /** + * @return string|null + */ + public function getTo() + { + return $this->to; + } + + /** + * The notification object to send to FCM. `title` and `body` are required. + * @param array $params ['title' => '', 'body' => '', 'sound' => '', 'icon' => '', 'click_action' => ''] + * @return $this + */ + public function content(array $params) + { + $this->notification = $params; + + return $this; + } + + /** + * @param array|null $data + * @return $this + */ + public function data($data = null) + { + $this->data = $data; + + return $this; + } + + /** + * @param string $priority + * @return $this + */ + public function priority($priority) + { + $this->priority = $priority; + + return $this; + } + + /** + * @return string + */ + public function formatData() + { + return json_encode([ + 'data' => $this->data, + 'notification' => $this->notification, + 'priority' => $this->priority, + 'to' => $this->to + ]); + } +} \ No newline at end of file diff --git a/src/FcmNotificationServiceProvider.php b/src/FcmNotificationServiceProvider.php new file mode 100755 index 0000000..b37a9eb --- /dev/null +++ b/src/FcmNotificationServiceProvider.php @@ -0,0 +1,36 @@ +publishes([ + __DIR__.'/config/laravel-fcm-notification.php' => config_path('laravel-fcm-notification.php'), + ]); + } + + /** + * Register + */ + public function register() + { + $app = $this->app; + $this->app->make(ChannelManager::class)->extend('fcm', function() use ($app) { + return $app->make(FcmChannel::class); + }); + } +} \ No newline at end of file diff --git a/src/config/laravel-fcm-notification.php b/src/config/laravel-fcm-notification.php new file mode 100644 index 0000000..1de74ed --- /dev/null +++ b/src/config/laravel-fcm-notification.php @@ -0,0 +1,8 @@ + '' +]; \ No newline at end of file