diff --git a/README.md b/README.md index e06306b..5c246a4 100644 --- a/README.md +++ b/README.md @@ -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 + } } ``` diff --git a/composer.json b/composer.json index 2ea8820..1e21970 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/FcmChannel.php b/src/FcmChannel.php index 7b78344..f2dfcab 100644 --- a/src/FcmChannel.php +++ b/src/FcmChannel.php @@ -2,14 +2,13 @@ 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; @@ -17,29 +16,35 @@ 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) { @@ -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 { @@ -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) @@ -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)