Skip to content

Commit

Permalink
drop firebase and replace it with filament-fcm package
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Jun 24, 2024
1 parent 3ee9bc8 commit e25873c
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 546 deletions.
1 change: 0 additions & 1 deletion .stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Notification
{
public function sendToFCM(Model $user, array $data=[]): static {}
public function sendToEmail(Model $user): static {}
public function sendToSlack(Model $user): static {}
public function sendToDiscord(Model $user): static {}
Expand Down
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,43 @@ to make FCM Notification Work you need to install [Filament Settings Hub](https:
)
```

now go to settings hub and update FCM settings by add admin-auth.json file and update fcm settings than run this command
than you need to install `filament-fcm` package by use this command

```bash
php artisan filament-alerts:fcm
composer require tomatophp/filament-fcm
```

and add the service provider plugin

```php
->plugin(\TomatoPHP\FilamentFcm\FilamentFcmPlugin::make())
```

now you need to update config


```dotenv
# Firebase Project
FIREBASE_API_KEY=
FIREBASE_AUTH_DOMAIN=
FIREBASE_DATABASE_URL=
FIREBASE_PROJECT_ID=
FIREBASE_STORAGE_BUCKET=
FIREBASE_MESSAGING_SENDER_ID=
FIREBASE_APP_ID=
FIREBASE_MEASUREMENT_ID=
# Firebase Cloud Messaging
FIREBASE_VAPID=
# Firebase Alert Sound
FCM_ALERT_SOUND=
```

than run this command

```bash
php artisan filament-fcm:install
```

it will generate FCM worker for you to make notifications working on the background.
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"filament/spatie-laravel-translatable-plugin": "^3.2",
"tomatophp/filament-settings-hub": "^1.0",
"tomatophp/discord-notifications": "^1.0",
"tomatophp/fcm-notifications": "^1.0",
"tomatophp/pusher-notifications": "^1.0",
"tomatophp/messagebird-notifications": "^1.0",
"laravel/slack-notification-channel": "^3.2"
Expand Down
60 changes: 0 additions & 60 deletions src/Console/FilamentAlertsFCM.php

This file was deleted.

60 changes: 13 additions & 47 deletions src/FilamentAlertsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public function getId(): string
"name" => 'Discord',
"id" => "discord"
],
[
"name" => 'FCM Web',
"id" => "fcm-web"
],
[
"name" => 'FCM Mobile',
"id" => "fcm-api"
],
[
"name" => 'Reverb',
"id" => "reverb"
Expand Down Expand Up @@ -271,45 +263,6 @@ public function boot(Panel $panel): void
Config::set('filament-alerts.lang', count($this->lang)?$this->lang:[]);
Config::set('filament-alerts.types', count($this->types)?$this->types:[]);

if($this->useFCM){
FilamentView::registerRenderHook(
PanelsRenderHook::BODY_END,
function (){
return view('filament-alerts::firebase');
},
);

Notification::macro('sendToFCM', function (Model $user, array $data=[]): static
{
/** @var Notification $this */
$user->notifyFCMSDK(
title: $this->title,
message: $this->body,
type: 'fcm-web',
url: count($this->actions)? $this->actions[0]->getUrl() : null,
icon: $this->icon,
data: [
'url' => count($this->actions)? $this->actions[0]->getUrl() : null,
'id' => $this->getId(),
'actions' => array_map(fn (Action | ActionGroup $action): array => $action->toArray(), $this->getActions()),
'body' => $this->getBody(),
'color' => $this->getColor(),
'duration' => $this->getDuration(),
'icon' => $this->getIcon(),
'iconColor' => $this->getIconColor(),
'status' => $this->getStatus(),
'title' => $this->getTitle(),
'view' => $this->getView(),
'viewData' => $this->getViewData(),
'data'=> $data
]
);

return $this;
});
}


if($this->useEmail){
Notification::macro('sendToEmail', function (Model $user): static
{
Expand Down Expand Up @@ -365,6 +318,19 @@ function (){
return $this;
});
}

if($this->useFCM){
$this->providers = array_merge($this->providers, [
[
"name" => 'FCM Web',
"id" => "fcm-web"
],
[
"name" => 'FCM Mobile',
"id" => "fcm-api"
],
]);
}
}

public static function make(): static
Expand Down
4 changes: 0 additions & 4 deletions src/FilamentAlertsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function register(): void
//Register generate command
$this->commands([
\TomatoPHP\FilamentAlerts\Console\FilamentAlertsInstall::class,
\TomatoPHP\FilamentAlerts\Console\FilamentAlertsFCM::class,
]);

//Register Config file
Expand Down Expand Up @@ -61,9 +60,6 @@ public function register(): void

//Register Routes
$this->loadRoutesFrom(__DIR__.'/../routes/api.php');

Livewire::component(Firebase::class);

}

public function boot(): void
Expand Down
35 changes: 28 additions & 7 deletions src/Jobs/NotificationJop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TomatoPHP\FilamentAlerts\Jobs;

use Filament\Notifications\Actions\Action;
use Filament\Notifications\Actions\ActionGroup;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
Expand Down Expand Up @@ -57,13 +59,6 @@ public function __construct($arrgs)
*/
public function handle()
{
if ($this->provider === 'fcm-api' || $this->provider === 'fcm-web') {
$this->user->fcm = $this->provider;
$this->user->fcmId = $this->user->id;
} else {
$this->user->fcm = "fcm-api";
$this->user->fcmId = $this->user->id;
}

if($this->provider === 'sms-misr'){
if($this->user->phone){
Expand All @@ -81,6 +76,32 @@ public function handle()
Http::post('https://smsmisr.com/api/SMS', $params)->json();
}
}
else if ($this->provider === 'fcm-api' || $this->provider === 'fcm-web') {
$this->user->notifyFCMSDK(
message: $this->message,
type: $this->provider,
title: $this->title,
url: $this->url,
image: $this->image,
icon: $this->icon,
data: [
'url' => $this->url,
'id' => $this->model_id,
'actions' => [],
'body' => $this->message,
'color' => null,
'duration' => null,
'icon' => $this->icon,
'iconColor' => null,
'status' => null,
'title' => $this->title,
'view' => null,
'viewData' => null,
'data'=> $this->data
],
sendToDatabase: false
);
}
else {
$this->user->notify(new NotificationService(
$this->title,
Expand Down
76 changes: 0 additions & 76 deletions src/Jobs/NotifyFCMJob.php

This file was deleted.

Loading

0 comments on commit e25873c

Please sign in to comment.