Skip to content

Commit

Permalink
Add support for laravel-firebase 3.0 (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Janssen <[email protected]>
  • Loading branch information
dododedodonl and dododedodonl authored Dec 4, 2020
1 parent 8b95cd0 commit 58fbccd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class AccountActivated extends Notification
ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
}

// optional method when using kreait/laravel-firebase:^3.0, this method can be omitted, defaults to the default project
public function fcmProject($notifiable, $message)
{
// $message is what is returned by `toFcm`
return 'app'; // name of the firebase project to use
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"guzzlehttp/guzzle": "^6.2 || ^7.0",
"illuminate/notifications": "~5.6 || ~6.0 || ~7.0 || ~8.0",
"illuminate/support": "~5.6 || ~6.0 || ~7.0 || ~8.0",
"kreait/laravel-firebase": "^1.3 || ^2.1",
"kreait/laravel-firebase": "^1.3 || ^2.1 || ^3.0",
"spatie/enum": "^2.3 || ^3.0"
},
"require-dev": {
Expand Down
56 changes: 40 additions & 16 deletions src/FcmChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,49 @@

namespace NotificationChannels\Fcm;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Notification;
use Kreait\Firebase\Exception\MessagingException;
use Kreait\Firebase\Messaging as MessagingClient;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Message;
use Kreait\Laravel\Firebase\Facades\FirebaseMessaging;
use NotificationChannels\Fcm\Exceptions\CouldNotSendNotification;
use Throwable;

class FcmChannel
{
const MAX_TOKEN_PER_REQUEST = 500;

/***
* @var Dispatcher
/**
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;

/**
* FcmChannel constructor.
*
* @param Dispatcher $dispatcher
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
*/
public function __construct(Dispatcher $dispatcher)
{
$this->events = $dispatcher;
}

/**
* @var string|null
*/
protected $fcmProject = null;

/**
* Send the given notification.
*
* @param mixed $notifiable
* @param Notification $notification
* @param \Illuminate\Notifications\Notification $notification
*
* @return array
* @throws CouldNotSendNotification|\Kreait\Firebase\Exception\FirebaseException
* @throws \NotificationChannels\Fcm\Exceptions\CouldNotSendNotification
* @throws \Kreait\Firebase\Exception\FirebaseException
*/
public function send($notifiable, Notification $notification)
{
Expand All @@ -56,6 +61,11 @@ public function send($notifiable, Notification $notification)
throw CouldNotSendNotification::invalidMessage();
}

$this->fcmProject = null;
if (method_exists($notification, 'fcmProject')) {
$this->fcmProject = $notification->fcmProject($notifiable, $fcmMessage);
}

$responses = [];

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

/**
* @param Message $fcmMessage
* @return \Kreait\Firebase\Messaging
*/
protected function messaging()
{
try {
$messaging = app('firebase.manager')->project($this->fcmProject)->messaging();
} catch (BindingResolutionException $e) {
$messaging = app('firebase.messaging');
}

return $messaging;
}

/**
* @param \Kreait\Firebase\Messaging\Message $fcmMessage
* @param $token
* @return array
* @throws MessagingException
* @throws \Kreait\Firebase\Exception\MessagingException
* @throws \Kreait\Firebase\Exception\FirebaseException
*/
protected function sendToFcm(Message $fcmMessage, $token)
Expand All @@ -93,27 +117,27 @@ protected function sendToFcm(Message $fcmMessage, $token)
$fcmMessage->setToken($token);
}

return FirebaseMessaging::send($fcmMessage);
return $this->messaging()->send($fcmMessage);
}

/**
* @param $fcmMessage
* @param array $tokens
* @return MessagingClient\MulticastSendReport
* @throws MessagingException
* @return \Kreait\Firebase\Messaging\MulticastSendReport
* @throws \Kreait\Firebase\Exception\MessagingException
* @throws \Kreait\Firebase\Exception\FirebaseException
*/
protected function sendToFcmMulticast($fcmMessage, array $tokens)
{
return FirebaseMessaging::sendMulticast($fcmMessage, $tokens);
return $this->messaging()->sendMulticast($fcmMessage, $tokens);
}

/**
* Dispatch failed event.
*
* @param $notifiable
* @param Notification $notification
* @param Throwable $exception
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param \Throwable $exception
* @return array|null
*/
protected function failedNotification($notifiable, Notification $notification, Throwable $exception)
Expand Down

0 comments on commit 58fbccd

Please sign in to comment.