Notification System
The system features a multi-channel notification architecture that supports real-time alerts, email delivery, and persistent database storage.
1. Architecture
Sender (Controller/Service)
│
▼
NotificationService::send()
│
├─→ Database Channel (Persist to DB)
├─→ Email Channel (Send via SMTP)
└─→ Real-time (via Polling/WebSocket)
Key Components
- Notification Entity: Represents a single alert message.
- NotificationRepository: Handles database storage & retrieval.
- NotificationService: Orchestrates dispatching to channels.
- Polling System: Client-side JS that checks for new alerts.
2. Sending Notifications
Via Service
$service = service('notification');
$service->send(
'New Order Received', // Title
'Order #1234 has been placed.', // Message
'order_created', // Type/Event
$userId, // Recipient ID
null, // Related Entity ID (optional)
'orders' // Related Entity Type (optional)
);With Media Attachments
$service->send(
'Proof of Delivery',
'Here is the photo.',
'delivery',
$userId,
null,
null,
$imagePath, // Image URL/Path
$filePath // File URL/Path
);
$filePath // File URL/Path
);3. Export Functionality
Service Layer
The NotificationService handles exporting notifications to Excel and PDF formats.
$service = service('notification');
// Returns keys: content, filename, mime
$file = $service->exportNotifications($userId, $roleIds, 'excel'); // or 'pdf'UI Integration
Exports are handled via Client-Side Download to ensure robust file handling.
<!-- Export Button -->
<?= component('button', [
'onclick' => "downloadExport('/admin/notifications/export/excel')",
'label' => 'Excel',
'class' => 'btn btn-outline-success btn-sm',
'icon' => 'fas fa-file-excel'
]) ?>3. Client-Side Integration
Notification Bell
The notification bell in the topbar automatically updates when new notifications arrive.
<!-- Renders the notification bell icon with unread count -->
<?= ajax_component('notification_bell', [], true, 'bell-1', 30000) ?>Polling Script
Located in public/assets/js/components/notification-polling.js.
- Polls
/api/notifications/pollevery 30 seconds (configurable). - Updates the notification counter.
- Plays sound (optional) or shows toast on new alerts.