Installation:
composer require tomatophp/filament-alerts
Publish the config file:
php artisan vendor:publish --provider="TomatoPHP\FilamentAlerts\FilamentAlertsServiceProvider" --tag="filament-alerts-config"
Configure Channels:
Edit config/filament-alerts.php to define your notification channels (e.g., mail, database, fcm, filament). Example:
'channels' => [
'mail' => [
'enabled' => true,
'from' => 'no-reply@example.com',
],
'fcm' => [
'enabled' => true,
'api_key' => env('FCM_API_KEY'),
],
],
First Use Case: Send a notification via Filament’s native UI:
use TomatoPHP\FilamentAlerts\Facades\FilamentAlerts;
FilamentAlerts::send(
user: $user,
template: 'welcome', // Matches a template in `resources/views/vendor/filament-alerts/`
data: ['name' => $user->name]
);
Define a Template:
Create a Blade template at resources/views/vendor/filament-alerts/welcome.blade.php:
<h1>Welcome, {{ $data['name'] }}!</h1>
<p>You’ve successfully registered.</p>
Template-Based Notifications:
resources/views/vendor/filament-alerts/ (e.g., order_confirmation.blade.php).data array:
FilamentAlerts::send($user, 'order_confirmation', ['order_id' => 123]);
Multi-Channel Dispatch:
FilamentAlerts::send($user, 'template_name');
FilamentAlerts::send($user, 'template_name', [], ['channels' => ['mail', 'fcm']]);
Filament UI Integration:
filament-alerts:send command in Filament actions/resources:
use TomatoPHP\FilamentAlerts\Actions\SendAlertAction;
public static function getActions(): array
{
return [
SendAlertAction::make('Notify User')
->template('welcome')
->data(['name' => 'John']),
];
}
FCM (Firebase) Notifications:
fcm channel in filament-alerts.php:
'fcm' => [
'enabled' => true,
'api_key' => env('FCM_SERVER_KEY'),
'topic' => 'user_updates', // Optional: broadcast to a topic
],
Macros for Custom Logic: Extend Filament’s notification service with macros:
use TomatoPHP\FilamentAlerts\FilamentAlerts;
FilamentAlerts::extend(function ($notifier) {
$notifier->macro('urgent', function ($user, $template, $data = []) {
return $notifier->send($user, $template, array_merge($data, ['urgent' => true]));
});
});
// Usage:
FilamentAlerts::urgent($user, 'alert', ['message' => 'Critical update!']);
Laravel Notifications:
Leverage existing Laravel notification classes for complex logic, then pass them to FilamentAlerts:
$notification = (new OrderShipped($order))->via(['mail']);
FilamentAlerts::send($user, 'order_shipped', [], ['notification' => $notification]);
Queue Delayed Notifications: Use Laravel queues to delay notifications:
FilamentAlerts::later(now()->addHours(1), function () use ($user) {
FilamentAlerts::send($user, 'reminder', ['task' => 'Complete profile']);
});
Dynamic Templates: Fetch templates dynamically from a database or API:
$templateContent = Cache::get("template_{$templateName}");
FilamentAlerts::send($user, null, [], ['content' => $templateContent]);
Testing:
Mock the FilamentAlerts facade in tests:
FilamentAlerts::shouldReceive('send')->once()->with($user, 'template', ['data']);
Template Paths:
resources/views/vendor/filament-alerts/ or they won’t be found.php artisan view:clear
FCM Configuration:
service-worker.js must be registered in your frontend (check browser console for errors).FCM_SERVER_KEY, ensure it’s a Server Key (not a Legacy Key) in Firebase Console.Channel Priority:
FilamentAlerts override Filament’s native notifications if both are configured for the same event.'filament' => [
'enabled' => false,
],
Data Binding:
data array passed to send(). Avoid relying on global variables or session data.array_merge to combine default and dynamic data:
FilamentAlerts::send($user, 'template', ['default' => 'value'], ['data' => ['override' => 'value']]);
Rate Limiting:
'fcm' => [
'enabled' => true,
'log_responses' => true, // Add this to config
],
Check Logs:
Enable debug mode in filament-alerts.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
FCM Debugging: Use the FCM DebugView to inspect sent messages.
Template Errors: Wrap template rendering in a try-catch to avoid crashes:
try {
FilamentAlerts::send($user, 'template');
} catch (\Exception $e) {
Log::error("Template error: " . $e->getMessage());
// Fallback to a plain text notification
$user->notify(new PlainTextNotification("Fallback message"));
}
Custom Channels:
Extend the TomatoPHP\FilamentAlerts\Channels\Channel class to add new channels (e.g., Slack, SMS):
namespace App\Notifications\Channels;
use TomatoPHP\FilamentAlerts\Channels\Channel;
class SlackChannel extends Channel
{
public function send($notifiable, $template, $data)
{
// Custom logic to send to Slack
}
}
Register it in config/filament-alerts.php:
'channels' => [
'slack' => [
'enabled' => true,
'class' => \App\Notifications\Channels\SlackChannel::class,
],
],
Template Preprocessing: Add a macro to preprocess template data:
FilamentAlerts::extend(function ($notifier) {
$notifier->macro('preprocess', function ($user, $template, $data = []) {
$data['processed_at'] = now()->format('Y-m-d H:i');
return $notifier->send($user, $template, $data);
});
});
Event Listeners: Trigger notifications from Laravel events:
use TomatoPHP\FilamentAlerts\Facades\FilamentAlerts;
event(new UserRegistered($user));
FilamentAlerts::send($user, 'welcome');
Filament Policy Integration: Restrict who can send alerts via Filament policies:
public static function canAccess(): bool
{
return auth()->
How can I help you explore Laravel packages today?