Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
benwilkins committed Dec 6, 2016
1 parent 8337cb8 commit 697153b
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 0 deletions.
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "benwilkins/laravel-fcm-notification",
"description": "Laravel FCM (Firebase Cloud Messaging) Notification Channel",
"authors": [
{
"name": "Ben Wilkins",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"guzzlehttp/guzzle": "^6.2"
},
"autoload": {
"psr-4": {
"Benwilkins\\FCM\\": "src/"
}
}
}
65 changes: 65 additions & 0 deletions src/FcmChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Benwilkins\FCM;

use GuzzleHttp\Client;
use Illuminate\Notifications\Notification;

/**
* Class FcmChannel
* @package Benwilkins\FCM
*/
class FcmChannel
{
/**
* @const The API URL for Firebase
*/
const API_URI = 'https://fcm.googleapis.com/fcm/send';

/**
* @var Client
*/
private $client;

/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->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');
}
}
101 changes: 101 additions & 0 deletions src/FcmMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Benwilkins\FCM;

/**
* Class FcmMessage
* @package Benwilkins\FCM
*/
class FcmMessage
{
const PRIORITY_NORMAL = 'normal';
const PRIORITY_HIGH = 'high';

/**
* @var string
*/
private $to;
/**
* @var array
*/
private $notification;
/**
* @var array
*/
private $data;
/**
* @var string normal|high
*/
private $priority = self::PRIORITY_NORMAL;

/**
* @param string $recipient
* @param bool $recipientIsTopic
* @return $this
*/
public function to($recipient, $recipientIsTopic = false)
{
if ($recipientIsTopic) {
$this->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
]);
}
}
36 changes: 36 additions & 0 deletions src/FcmNotificationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Benwilkins\FCM;

use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\ServiceProvider;

/**
* Class FcmNotificationServiceProvider
* @package Benwilkins\FCM
*/
class FcmNotificationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->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);
});
}
}
8 changes: 8 additions & 0 deletions src/config/laravel-fcm-notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
/*
* Add the Firebase API key
*/
'api_key' => ''
];

0 comments on commit 697153b

Please sign in to comment.