Skip to content

Commit 35bb32e

Browse files
committed
First
0 parents  commit 35bb32e

File tree

6 files changed

+470
-0
lines changed

6 files changed

+470
-0
lines changed

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "liliom/laravel-firebase",
3+
"description": "Laravel FCM (Firebase Cloud Messaging) Notification Channel",
4+
"keywords": [
5+
"laravel",
6+
"fcm",
7+
"firebase",
8+
"notification",
9+
"channel",
10+
"notification-channel"
11+
],
12+
"authors": [
13+
{
14+
"name": "Hussam Abd",
15+
"email": "hussam3bd@liliom.co"
16+
}
17+
],
18+
"license": "MIT",
19+
"require": {
20+
"php": ">=5.5.9",
21+
"illuminate/support": "5.*",
22+
"guzzlehttp/guzzle": "~6.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Liliom\\Firebase\\": "src/"
27+
}
28+
},
29+
"extra": {
30+
"laravel": {
31+
"providers": [
32+
"Liliom\\Firebase\\FirebaseServiceProvider"
33+
]
34+
}
35+
},
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Liliom\Firebase\Exceptions;
4+
5+
class CouldNotSendNotification extends \Exception
6+
{
7+
/**
8+
* Thrown when recipient is missing.
9+
*
10+
* @return static
11+
*/
12+
public static function missingRecipient()
13+
{
14+
return new static('Notification was not sent. You should specify device token(s) for sending notification.');
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Liliom\Firebase\Exceptions;
4+
5+
class InvalidConfiguration extends \Exception
6+
{
7+
/**
8+
* Thrown when configuration key not set.
9+
*
10+
* @return static
11+
*/
12+
public static function configurationNotSet()
13+
{
14+
return new static('In order to send notification via Firebase you need to add credentials in the `firebase` key of `config.services`.');
15+
}
16+
}

src/FirebaseChannel.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Liliom\Firebase;
4+
5+
use Liliom\Firebase\Exceptions\CouldNotSendNotification;
6+
use Liliom\Firebase\Exceptions\InvalidConfiguration;
7+
use Illuminate\Notifications\Notification;
8+
use GuzzleHttp\Client;
9+
10+
class FirebaseChannel
11+
{
12+
/**
13+
* The API URL for Firebase
14+
*/
15+
const API_URI = 'https://fcm.googleapis.com/fcm/send';
16+
17+
/**
18+
* @var Client
19+
*/
20+
private $client;
21+
22+
/**
23+
* @param Client $client
24+
*/
25+
public function __construct(Client $client)
26+
{
27+
$this->client = $client;
28+
}
29+
30+
/**
31+
* Send the given notification.
32+
*
33+
* @param mixed $notifiable
34+
* @param \Illuminate\Notifications\Notification $notification
35+
*
36+
* @throws \Liliom\Firebase\Exceptions\CouldNotSendNotification
37+
*/
38+
public function send($notifiable, Notification $notification)
39+
{
40+
$message = $notification->toFirebase($notifiable);
41+
42+
if ($message->recipientNotGiven()) {
43+
if (!$to = $notifiable->routeNotificationFor('firebase')) {
44+
throw CouldNotSendNotification::missingRecipient();
45+
}
46+
47+
$message->to($to);
48+
}
49+
50+
$this->client->post(self::API_URI, [
51+
'headers' => [
52+
'Authorization' => 'key=' . $this->getApiKey(),
53+
'Content-Type' => 'application/json',
54+
],
55+
'body' => $message->payload(),
56+
]);
57+
}
58+
59+
/**
60+
* @return string
61+
*/
62+
private function getApiKey()
63+
{
64+
$key = config('services.firebase.key');
65+
if (is_null($key)) {
66+
throw InvalidConfiguration::configurationNotSet();
67+
}
68+
69+
return $key;
70+
}
71+
}

0 commit comments

Comments
 (0)