Skip to content

Notification Examples

The admin panel at admin/notifications/emails/{uri} shows rendered previews of notifications grouped by directory.

How it works

  1. A notification is sent → Laravel fires NotificationSent
  2. CacheNotificationContent listener builds a MailViewModel/SmsViewModel and saves it to disk
  3. Admin visits the panel → controller finds notification classes in the matching directory, reads their cached ViewModels, renders them via a presenter, and outputs HTML

Only notifications implementing the ShouldCacheContent marker interface participate.

Key classes

ResponsibilityClass
Find notification classes by directoryNotifications\Support\Finder\ComposerAutoloadNotificationClassFinder
Cache on send (queued listener)Modules\Notification\Listeners\CacheNotificationContent
Build ViewModel from notificationModules\Notification\CachedContent\Factory\DefaultNotificationViewModelFactory
Store/retrieve ViewModels on diskModules\Notification\CachedContent\Store\FilesystemNotificationCachedContentStore
Render ViewModel to HTMLModules\Notification\CachedContent\Presenter\DefaultNotificationViewModelPresenter
Generate admin panel links from FQCNModules\Notification\CachedContent\NotificationCachedContentLink

Adding a notification to the panel

  1. Implement ShouldCacheContent on the notification (or extend MailNotification/InAppNotification which already implement it)
  2. Add metadata attributes (TriggerMode, TriggerConditions, etc.)
  3. Send it once to populate the cache (see below)

Creating an example

The cache is populated by the CacheNotificationContent listener whenever a notification is actually sent. The easiest way to trigger this locally is via Tinker:

php
// 1. Pick a member to act as recipient
$member = \App\Modules\Member\Models\Member::find(1);

// 2. Instantiate the notification with its required dependencies
$enrollment = $member->courseEnrollments()->first();
$notification = new \App\Notifications\Course\Enrollment\CourseCertificateGenerated($enrollment);

// 3. Send it — the CacheNotificationContent listener will fire automatically
$member->notify($notification);

After sending, the cached example appears on the matching admin panel page (e.g. admin/notifications/emails/course/enrollment).