Notifiable Entities
Learn how to configure entities and models to receive notifications and manage in-app alerts.
The Notifiable Trait
To allow any CodeIgniter 4 Model or Entity to receive notifications, import the Jengo\Notifications\Concerns\Notifiable trait:
namespace App\Entities;
use Jengo\Base\Entities\BaseEntity;
use Jengo\Notifications\Concerns\Notifiable;
class User extends BaseEntity
{
use Notifiable;
/**
* Custom route for SMS notifications.
*/
public function routeNotificationForSms(): ?string
{
return $this->attributes['phone_number'] ?? null;
}
/**
* Custom route for WebPush/FCM notifications.
*/
public function routeNotificationForPush(): ?string
{
return $this->attributes['fcm_token'] ?? null;
}
}Routing Customization
By default, the notification engine checks standard property names on your entity to find the recipient address:
| Channel | Default Entity Attribute | Custom Route Method |
|---|---|---|
mail | $entity->email | routeNotificationForMail(): string|array |
sms | $entity->phone_number, $entity->phone, $entity->mobile | routeNotificationForSms(): string |
push | $entity->device_token, $entity->fcm_token, $entity->push_subscription | routeNotificationForPush(): string|array |
slack | $entity->slack_webhook_url | routeNotificationForSlack(): string |
webhook | $entity->webhook_url | routeNotificationForWebhook(): string |
Example: Multi-Recipient Mail Routing
public function routeNotificationForMail(): array
{
return [$this->email, 'backup-' . $this->email];
}Managing In-App Database Notifications
The Notifiable trait includes helper methods to retrieve and mutate notifications stored in the database:
Querying Notifications
// Retrieve all notifications
$all = $user->notifications();
// Retrieve only unread notifications
$unread = $user->unreadNotifications();
// Retrieve only read notifications
$read = $user->readNotifications();Accessing Alert Details
Each item returned is an instance of Jengo\Notifications\Entities\DatabaseNotification:
foreach ($user->unreadNotifications() as $notification) {
echo $notification->title(); // Title string
echo $notification->message(); // Alert message
echo $notification->link(); // Actionable URL
echo $notification->actionText(); // Button label
echo $notification->level(); // 'info', 'success', 'warning', 'danger'
echo $notification->created_at; // Timestamp
// Mark single notification as read
$notification->markAsRead();
}Bulk Marking as Read
// Mark all unread notifications for this user as read
$user->markAsRead();Conditional Delivery (shouldSend)
You can control whether a notification should be delivered to a specific recipient or channel by overriding shouldSend() on the Notification class:
class MonthlySummary extends Notification
{
public function via(object $notifiable): array
{
return ['mail', 'database', 'sms'];
}
public function shouldSend(object $notifiable, string $channel): bool
{
// Don't send SMS to users who have opted out in user preferences
if ($channel === 'sms' && ! ($notifiable->sms_enabled ?? false)) {
return false;
}
return true;
}
}On-Demand (Ad-Hoc) Notifiables
When you need to deliver a notification to a recipient who does not exist in your database table (such as a guest customer or external billing endpoint), use Notification::route():
use Jengo\Notifications\Notification;
Notification::route('mail', 'guest@example.com')
->route('sms', '+254700000000')
->notify(new OrderConfirmation($order));