audentio/laravel-notifications
Composer 安装命令:
composer require audentio/laravel-notifications
包简介
Base notifications and notification preferences system for Audentio platforms.
README 文档
README
Notifications and notification preference system for Audentio Laravel platforms. Provides a database-backed notification preference system with per-channel defaults, per-tenant overrides, per-user overrides, a GraphQL API, and optional push notification support.
Requirements
- PHP 8.1+
- Laravel 12
audentio/laravel-base
Installation
composer require audentio/laravel-notifications
Publish and run the migrations:
php artisan vendor:publish --tag=audentio-notifications-config php artisan migrate
Publish the model stubs:
php artisan vendor:publish --tag=audentio-notifications-models
Setup
1. Implement models
The published stubs in app/Models/ need to be wired up to your application. At minimum:
NotificationPreference — extend getDefaultUserNotificationPreferenceValue() if needed. The getName() method must return a human-readable label (typically from a translation key).
NotificationPreferenceGroup — same, getName() must be implemented.
UserNotificationPreference — no changes needed beyond the stub.
2. Add the trait to your User model
use Audentio\LaravelNotifications\Models\Interfaces\NotifiableUserInterface; use Audentio\LaravelNotifications\Models\Traits\NotifiableUserTrait; class User extends Model implements NotifiableUserInterface { use NotifiableUserTrait; public function isEmailVerified(): bool { return $this->email_verified_at !== null; } }
3. Create notification handlers
Extend AbstractNotification and implement getNotificationPreferenceId():
use Audentio\LaravelNotifications\Notifications\AbstractNotification; class NewMessageNotification extends AbstractNotification { public function getNotificationPreferenceId(): string { return 'newMessage'; } public function toMail($notifiable): MailMessage { return (new MailMessage)->subject('New message'); } }
4. Seed preferences
Run your NotificationPreferencesAndGroupsTableSeeder after adding handlers. The seeder auto-discovers handlers and creates NotificationPreference records.
Notification Preference Hierarchy
User preferences follow a 3-level fallback (highest priority first):
| Level | Source | Stored in |
|---|---|---|
| 3 | User override | user_notification_preferences.disabled_channels |
| 2 | Tenant override | App-provided via getNotificationPreferenceTenantDefaults() |
| 1 | Global default | notification_preferences.default_disabled_channels |
When no record exists at a level, the next level is used. An empty disabled_channels array means all channels are enabled.
Setting global defaults
Set default_disabled_channels when seeding a preference:
'streamGoLive' => [ 'available_channels' => ['notification', 'mail'], 'default_disabled_channels' => ['mail'], // mail off by default for all users ],
Tenant-level overrides
Override getNotificationPreferenceTenantDefaults() on your User model to load tenant-specific defaults. This is called once per getUserNotificationPreferenceValues() call — load all tenant preferences in a single query:
public function getNotificationPreferenceTenantDefaults(): Collection { $tenant = $this->getCurrentTenant(); if (!$tenant) { return collect(); } return TenantNotificationPreference::where('tenant_id', $tenant->id)->get(); }
Optional: Disable automatic migrations
If you manage migrations manually:
// In a service provider LaravelNotifications::skipMigrations();
Optional: Push notifications
Enable push support in config/audentioNotifications.php:
'push_enabled' => true, 'push_handler_classes' => [ 'expo' => \Audentio\LaravelNotifications\PushHandlers\ExpoPushHandler::class, 'fcm' => \Audentio\LaravelNotifications\PushHandlers\FirebasePushHandler::class, ],
Run the additional push migrations:
php artisan migrate
Push subscriptions are managed via the routeNotificationForPush() method on your User model (provided by NotifiableUserTrait — override if needed).
GraphQL
The package auto-registers a GraphQL schema when audentio/laravel-graphql is present. Exposed types, queries, and mutations include:
- Queries:
notifications,notificationPreferenceGroups - Mutations:
updateViewerNotificationPreferenceValue,dismissNotification,markReadNotification,markUnreadNotification,markReadAllNotifications,dismissAllNotifications - Types:
NotificationPreference,NotificationPreferenceGroup,UserNotificationPreferenceValue,NotificationChannelEnum
Override individual types via graphQL_schema_overrides in the config.
To disable GraphQL registration:
LaravelNotifications::skipGraphQLSchema();
Testing
composer test
License
MIT
audentio/laravel-notifications 适用场景与选型建议
audentio/laravel-notifications 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.13k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 audentio/laravel-notifications 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 audentio/laravel-notifications 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-22