Skip to content

Commit 58fbccd

Browse files
authored
Add support for laravel-firebase 3.0 (#65)
Co-authored-by: Tom Janssen <[email protected]>
1 parent 8b95cd0 commit 58fbccd

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class AccountActivated extends Notification
8383
ApnsConfig::create()
8484
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
8585
}
86+
87+
// optional method when using kreait/laravel-firebase:^3.0, this method can be omitted, defaults to the default project
88+
public function fcmProject($notifiable, $message)
89+
{
90+
// $message is what is returned by `toFcm`
91+
return 'app'; // name of the firebase project to use
92+
}
8693
}
8794
```
8895

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"guzzlehttp/guzzle": "^6.2 || ^7.0",
1717
"illuminate/notifications": "~5.6 || ~6.0 || ~7.0 || ~8.0",
1818
"illuminate/support": "~5.6 || ~6.0 || ~7.0 || ~8.0",
19-
"kreait/laravel-firebase": "^1.3 || ^2.1",
19+
"kreait/laravel-firebase": "^1.3 || ^2.1 || ^3.0",
2020
"spatie/enum": "^2.3 || ^3.0"
2121
},
2222
"require-dev": {

src/FcmChannel.php

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,49 @@
22

33
namespace NotificationChannels\Fcm;
44

5+
use Illuminate\Contracts\Container\BindingResolutionException;
56
use Illuminate\Contracts\Events\Dispatcher;
67
use Illuminate\Notifications\Events\NotificationFailed;
78
use Illuminate\Notifications\Notification;
89
use Kreait\Firebase\Exception\MessagingException;
9-
use Kreait\Firebase\Messaging as MessagingClient;
1010
use Kreait\Firebase\Messaging\CloudMessage;
1111
use Kreait\Firebase\Messaging\Message;
12-
use Kreait\Laravel\Firebase\Facades\FirebaseMessaging;
1312
use NotificationChannels\Fcm\Exceptions\CouldNotSendNotification;
1413
use Throwable;
1514

1615
class FcmChannel
1716
{
1817
const MAX_TOKEN_PER_REQUEST = 500;
1918

20-
/***
21-
* @var Dispatcher
19+
/**
20+
* @var \Illuminate\Contracts\Events\Dispatcher
2221
*/
2322
protected $events;
2423

2524
/**
2625
* FcmChannel constructor.
2726
*
28-
* @param Dispatcher $dispatcher
27+
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
2928
*/
3029
public function __construct(Dispatcher $dispatcher)
3130
{
3231
$this->events = $dispatcher;
3332
}
3433

34+
/**
35+
* @var string|null
36+
*/
37+
protected $fcmProject = null;
38+
3539
/**
3640
* Send the given notification.
3741
*
3842
* @param mixed $notifiable
39-
* @param Notification $notification
43+
* @param \Illuminate\Notifications\Notification $notification
4044
*
4145
* @return array
42-
* @throws CouldNotSendNotification|\Kreait\Firebase\Exception\FirebaseException
46+
* @throws \NotificationChannels\Fcm\Exceptions\CouldNotSendNotification
47+
* @throws \Kreait\Firebase\Exception\FirebaseException
4348
*/
4449
public function send($notifiable, Notification $notification)
4550
{
@@ -56,6 +61,11 @@ public function send($notifiable, Notification $notification)
5661
throw CouldNotSendNotification::invalidMessage();
5762
}
5863

64+
$this->fcmProject = null;
65+
if (method_exists($notification, 'fcmProject')) {
66+
$this->fcmProject = $notification->fcmProject($notifiable, $fcmMessage);
67+
}
68+
5969
$responses = [];
6070

6171
try {
@@ -77,10 +87,24 @@ public function send($notifiable, Notification $notification)
7787
}
7888

7989
/**
80-
* @param Message $fcmMessage
90+
* @return \Kreait\Firebase\Messaging
91+
*/
92+
protected function messaging()
93+
{
94+
try {
95+
$messaging = app('firebase.manager')->project($this->fcmProject)->messaging();
96+
} catch (BindingResolutionException $e) {
97+
$messaging = app('firebase.messaging');
98+
}
99+
100+
return $messaging;
101+
}
102+
103+
/**
104+
* @param \Kreait\Firebase\Messaging\Message $fcmMessage
81105
* @param $token
82106
* @return array
83-
* @throws MessagingException
107+
* @throws \Kreait\Firebase\Exception\MessagingException
84108
* @throws \Kreait\Firebase\Exception\FirebaseException
85109
*/
86110
protected function sendToFcm(Message $fcmMessage, $token)
@@ -93,27 +117,27 @@ protected function sendToFcm(Message $fcmMessage, $token)
93117
$fcmMessage->setToken($token);
94118
}
95119

96-
return FirebaseMessaging::send($fcmMessage);
120+
return $this->messaging()->send($fcmMessage);
97121
}
98122

99123
/**
100124
* @param $fcmMessage
101125
* @param array $tokens
102-
* @return MessagingClient\MulticastSendReport
103-
* @throws MessagingException
126+
* @return \Kreait\Firebase\Messaging\MulticastSendReport
127+
* @throws \Kreait\Firebase\Exception\MessagingException
104128
* @throws \Kreait\Firebase\Exception\FirebaseException
105129
*/
106130
protected function sendToFcmMulticast($fcmMessage, array $tokens)
107131
{
108-
return FirebaseMessaging::sendMulticast($fcmMessage, $tokens);
132+
return $this->messaging()->sendMulticast($fcmMessage, $tokens);
109133
}
110134

111135
/**
112136
* Dispatch failed event.
113137
*
114-
* @param $notifiable
115-
* @param Notification $notification
116-
* @param Throwable $exception
138+
* @param mixed $notifiable
139+
* @param \Illuminate\Notifications\Notification $notification
140+
* @param \Throwable $exception
117141
* @return array|null
118142
*/
119143
protected function failedNotification($notifiable, Notification $notification, Throwable $exception)

0 commit comments

Comments
 (0)