定制 amjitk/laravel-global-notification 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amjitk/laravel-global-notification

Composer 安装命令:

composer require amjitk/laravel-global-notification

包简介

A comprehensive notification system for Laravel with CMS and automated triggers.

README 文档

README

Global Notification Logo

Laravel Global Notification

A comprehensive notification system for Laravel applications. This package allows you to manage notification types and templates via a CMS and automatically trigger them based on system events.

Features

  • CMS Interface: Manage types, define variables, and test templates directly from the admin panel.
  • Multi-Channel Support: Configure different templates for different channels for the same event.
  • User Preference Center: Users can toggle specific notification channels on/off.
  • Auto-Triggering: Automatically fire notifications on Eloquent model events (created, updated, etc.).
  • In-App Toast Widget: Real-time polling widget to show notifications instantly.
  • User Notification Center: Built-in UI for users to view and manage their in-app notifications.
  • Dynamic Content: Use placeholders like {{name}} in your templates.

Installation

  1. Install the package via Composer:

    composer require amjitk/laravel-global-notification

    Note: If testing locally without Packagist, configure your composer.json repositories to point to the local path.

  2. Run Migrations:

    php artisan migrate
  3. (Optional) Publish Configuration and Assets:

    php artisan vendor:publish --tag=global-notification-config
    php artisan vendor:publish --tag=global-notification-views

Configuration

The configuration file config/global-notification.php allows you to customize:

  • Channels: Enabled channels (default: mail, database).
  • Route Prefix: URL prefix for the package routes (default: global-notification).
  • Middleware: Middleware for admin routes. Configurable via .env:
    GLOBAL_NOTIFICATION_AUTH_ENABLED=true # Set to false to disable authentication

Usage

1. Admin - Manage Notifications

Visit /global-notification/notification-types to create new Notification Types (e.g., order_placed).

  • Variables: Define available variables (e.g. order_id, amount) when creating a Type.
  • Templates: Create templates for each channel. Use the defined variables (e.g. {{ amount }}).
  • Testing: Use the "Send Test" button on any template to send a dummy notification to yourself immediately.

2. Triggering Notifications

Manual Trigger

Use the NotificationService to send a notification programmatically:

use AmjitK\GlobalNotification\Services\NotificationService;

$service = new NotificationService();
$service->send('order_placed', $user, ['order_id' => 123, 'amount' => '$50']);

Automatic Trigger (Model Events)

Add the AutoNotifyTrait to your Eloquent model:

use AmjitK\GlobalNotification\Traits\AutoNotifyTrait;

class Order extends Model
{
    use AutoNotifyTrait;

    // Map system events to Notification Type keys
    public $notificationRules = [
        'created' => 'order_placed',   // Fires 'order_placed' when Order is created
        'updated' => 'order_updated',  // Fires 'order_updated' when Order is updated
    ];
}

3. Notification Logs

View all system notifications at: /global-notification/logs

This page displays a global log of all notifications triggered in the system.

4. Scheduled Notifications

You can schedule notifications using the package's send method combined with Laravel's Scheduler. This creates a powerful workflow where you can define templates in the CMS types and trigger them via Cron jobs.

Why use the package's send method instead of Notification::send or Mail::send? Using $service->send() allows you to:

  1. Use CMS Templates: Manage the content of your scheduled emails/alerts dynamically without deploying code.
  2. Separate Logging: By tagging them with withSource('scheduled'), they appear in the dedicated Scheduled Logs dashboard, separating them from transactional or user alerts.

Example: Sending a Daily Digest

// app/Console/Kernel.php or app/Console/Commands/SendDailyDigest.php

use AmjitK\GlobalNotification\Services\NotificationService;

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        $service = app(NotificationService::class);
        $users = \App\Models\User::all();
        
        foreach ($users as $user) {
            // 'daily_digest' is a Notification Type defined in your CMS
            $service->withSource('scheduled')
                    ->send('daily_digest', $user, [
                        'date' => now()->toFormattedDateString(),
                        'stats' => $user->getDailyStats()
                    ]);
        }
    })->dailyAt('09:00');
}

5. In-App Widget (Toast)

To display a real-time toast notification in your application whenever a new notification arrives:

  1. Make sure you have published the views (or reference the package view).

  2. Add the following line to your main layout file (e.g., resources/views/layouts/app.blade.php), preferably just before the closing </body> tag:

    @include('global-notification::components.toast')
  3. Ensure your application layout includes Alpine.js or vanilla JS support (the component uses vanilla JS but allows for easy customization).

6. User Preferences

Users can manage their notification preferences (opt-in/out of specific channels) by visiting: /global-notification/preferences

  • The interface allows users to toggle channels (e.g., turn off Email for "Order Updates" but keep it for "Promotions").
  • The system automatically checks these preferences before sending any notification.

Testing

To run the package tests:

composer install
vendor/bin/phpunit

amjitk/laravel-global-notification 适用场景与选型建议

amjitk/laravel-global-notification 是一款 基于 Blade 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 amjitk/laravel-global-notification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 amjitk/laravel-global-notification 我们能提供哪些服务?
定制开发 / 二次开发

基于 amjitk/laravel-global-notification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: Blade

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18