Download the PHP package codinglabsau/laravel-notification-subscriptions without Composer
On this page you can find all versions of the php package codinglabsau/laravel-notification-subscriptions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codinglabsau/laravel-notification-subscriptions
More information about codinglabsau/laravel-notification-subscriptions
Files in codinglabsau/laravel-notification-subscriptions
Package laravel-notification-subscriptions
Short Description Manage user notification subscriptions across multiple notification types and channels
License MIT
Homepage https://github.com/codinglabsau/laravel-notification-subscriptions
Informations about the package laravel-notification-subscriptions
Laravel Notification Subscriptions
A Laravel package for managing user notification preferences across multiple channels. Let your users control how they receive notifications (email, in-app, push, Slack) while you maintain sensible defaults and rate limiting.
Features
- Channel-based subscriptions - Users can enable/disable notifications per channel
- Custom channels - Define your own channel enum with any channels you need (mail, database, Slack, Pusher, OneSignal, etc.)
- Per-notification control - Configure which channels each notification type supports
- Smart defaults - New users get sensible defaults; preferences are only stored when changed
- Rate limiting - Prevent notification spam with configurable per-channel rate limits
- Mandatory channels - Per-notification channels that users can't opt out of
- Simple API -
$user->getNotificationPreferences()and$user->updateNotificationPreferences()for settings UIs
Installation
1. Install via Composer
2. Publish and run migrations
3. Publish configuration (optional)
4. Create your channel enum
Create an enum that implements SubscribableChannel to define your notification channels:
5. Add trait to your User model
6. Publish and configure the service provider
Then register your subscribable notifications in app/Providers/NotificationSubscriptionsServiceProvider.php:
Don't forget to add this service provider to your bootstrap/providers.php:
Basic Usage
Creating a Subscribable Notification
Transform any Laravel notification into a subscribable notification by implementing the SubscribableNotification interface and using the DispatchesNotifications trait:
Dispatching Notifications
Use the static sendToSubscribers() method instead of Laravel's standard notification sending:
Transactional vs Subscribable Notifications
| Use Case | Method | Behavior |
|---|---|---|
| Transactional (password reset, order confirmation) | Standard Laravel $user->notify() |
Always sends, no filtering |
| Subscribable (messages, updates, marketing) | Notification::sendToSubscribers() |
Respects user preferences |
How It Works
When a notification is dispatched:
- Subscriber lookup - The
subscribers()method determines who should receive the notification - Channel filtering - For each subscriber, the package checks their preferences:
- If they have a stored preference for this notification type, only enabled channels are used
- If no preference exists, channels with
defaultOn() === trueare used
- Rate limiting - If a channel has rate limiting enabled and the notification has a subject, duplicate notifications are throttled
- Delivery - The notification is sent only to the appropriate channels
Channel Enum Reference
Your channel enum must implement SubscribableChannel with these methods:
| Method | Return Type | Description |
|---|---|---|
driver() |
string |
Laravel notification channel driver (e.g., 'database', 'mail', OneSignalChannel::class) |
label() |
string |
Human-readable label for UI (e.g., 'Email', 'Push Notifications') |
isEnabled() |
bool |
Whether this channel is currently available |
defaultOn() |
bool |
Whether new users have this channel enabled by default |
hasRateLimiting() |
bool |
Whether rate limiting applies to this channel |
rateLimitDuration() |
int |
Rate limit duration in seconds |
Example: Advanced Channel Configuration
Rate Limiting
Rate limiting prevents notification spam when the same notification could be triggered multiple times in quick succession.
How Rate Limiting Works
Rate limits are applied per combination of:
- Notification type (e.g.,
order_shipped) - Channel (e.g.,
mail) - Subject (the model that triggered the notification)
- Recipient (the user receiving the notification)
Subject Method
For rate limiting to work, your notification must return a subject:
If subject() returns null, rate limiting is skipped for that notification.
Mandatory Channels
Some notifications must always be sent via certain channels regardless of user preferences. For example, a support ticket reply might always need an email notification, even if the user has opted out of email for other notifications.
Defining Mandatory Channels
Override mandatoryChannels() in your notification class to specify channels that cannot be unsubscribed from:
By default, mandatoryChannels() returns an empty array, meaning all channels are optional.
How Mandatory Channels Work
Mandatory channels are enforced at three levels:
-
shouldSend()defense-in-depth — When dispatching a notification, mandatory channels always returntrueinshouldSend(), even if the user's subscription record excludes them. This ensures delivery even with stale subscription data. -
Validation re-injection — The
ValidatesNotificationPreferencestrait automatically re-injects mandatory channels into form requests duringprepareForValidation(), so they can never be removed by user input. NotificationPreferencesDTO — ThegetNotificationPreferences()method populates amandatoryproperty on the DTO, mapping each notification type to its mandatory channel values. This allows your UI to render mandatory channels as disabled/locked checkboxes.
Using Mandatory Data in the UI
The NotificationPreferences DTO includes a mandatory property:
Use this in your frontend to disable checkboxes for mandatory channels:
Building a Settings UI
The package provides a simple API for building notification preference UIs. The HasNotificationSubscriptions trait adds two methods to your User model:
getNotificationPreferences()- Returns a DTO withtypesandvaluesfor the UIupdateNotificationPreferences(array $preferences)- Updates preferences in the database
Controller Setup
The ValidatesNotificationPreferences trait:
- Generates validation rules for each registered notification type
- Automatically re-injects mandatory channels (users can't opt out of them)
The NotificationPreferences DTO
The getNotificationPreferences() method returns a NotificationPreferences object with three properties:
Example Blade Template
Inertia/Vue Example
Advanced Usage
Custom Notification Labels
Add metadata methods to your notifications for richer UIs:
Before Send Hook
Execute code before any notification is sent:
Custom Subscription Model
Extend the base model if you need additional functionality:
Database Schema
The package creates a notification_subscriptions table:
| Column | Type | Description |
|---|---|---|
| id | bigint | Primary key |
| user_id | bigint | Foreign key to users table |
| type | string | Notification type identifier |
| channels | json | Array of enabled channel names |
| created_at | timestamp | Creation timestamp |
| updated_at | timestamp | Last update timestamp |
A unique constraint ensures one subscription record per user/type combination.
Testing
Credits
- Coding Labs
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-notification-subscriptions with dependencies
illuminate/contracts Version ^12.0|^13.0
spatie/laravel-package-tools Version ^1.9.2