gl-package/notification-manager

Laravel 的通知管理包。

1.0.1 2024-07-26 11:48 UTC

This package is auto-updated.

Last update: 2024-09-30 09:55:03 UTC


README

这是一个用于通过 Telegram、WhatsApp 和 Email 管理通知的 Laravel 包。此包提供了一个统一的接口,用于通过流行的消息平台发送通知,使您的 Laravel 应用程序内的集成和通信更加流畅。它支持模板消息、队列发送和为每个通知通道自定义设置的丰富功能。

安装

要安装此包,请按照以下步骤操作:

  1. 通过 Composer 安装

    composer require gl-package/notification-manager
  2. 运行迁移

    php artisan migrate

    🔔 注意:在运行迁移之前,请确保正确配置您的数据库连接。

  3. /notification-manager/config URL 中配置您的通知设置

  4. 发布配置文件(可选,如果需要自定义配置页面)

    php artisan vendor:publish --provider="GlPackage\NotificationManager\Providers\NotificationManagerServiceProvider" --tag=notificationmanager-views
    php artisan vendor:publish --provider="GlPackage\NotificationManager\Providers\NotificationManagerServiceProvider" --tag=notificationmanager-controllers
    php artisan vendor:publish --provider="GlPackage\NotificationManager\Providers\NotificationManagerServiceProvider" --tag=notificationmanager-routes
  5. 配置队列(适用于 Laravel 11.0 之前的版本)

    🔔 注意:要启用 Laravel 10 和 11 中发送队列消息的排队,请确保在 config/queue.php 中配置了队列驱动程序,并运行 php artisan queue:table 创建必要的数据库表。然后,运行 php artisan migrate 应用迁移,并运行 php artisan queue:work 开始处理队列作业。

    php artisan queue:table
    php artisan migrate
    php artisan queue:work

使用方法

您可以在 Laravel 应用程序中使用以下方式使用通知类:

Telegram 通知

🔔 注意:请确保在您的 PHP 环境中已安装并启用了 cURL 扩展。在 Windows 上,在 php.ini 中取消注释 extension=curl。在 Ubuntu 上,使用 sudo apt-get install php-curl 安装它,并重新启动您的 web 服务器。

use GlPackage\NotificationManager\Notifications\TelegramNotification;

$data = [
    'message' => "<b>Hello World!</b> Click the button below:",
    'buttons' => [
        [
            ['text' => 'Click Me', 'url' => 'https://example.com'] // Optional , else pass [] array
        ]
    ]
];

$telegram = new TelegramNotification();
$telegram->quickSend($data); // For quick send
// or
$telegram->queueSend($data); // For queued send

电子邮件通知

use GlPackage\NotificationManager\Notifications\EmailNotification;
use Faker\Factory as Faker;

$data = [
    'to_address' => Faker::create()->email,
    'subject' => 'Welcome to Our Service',
    'body' => 'Thank you for signing up! Here are some details about your account.',
    'view' => 'notificationmanager.testmail', // Optional view page location
    'attachments' => [], // Optional attachments array
    'name' => 'Hello, Ajay' // Optional parameters
];

$mail = new EmailNotification();
$mail->send($data); // Send mail via queue
// Ensure to run: php artisan queue:work or use Supervisor

WhatsApp 通知

use GlPackage\NotificationManager\Notifications\WhatsAppNotification;

$data = [
    'mobile' => '91908*******',
    'template' => 'hello_world', // Sample template
    'parameters' => [ // Optional
        [
            "type" => "text",
            "text" => "Test"
        ],
        [
            "type" => "text",
            "text" => "1200"
        ]
    ],
    'buttons' => [ // Optional
        [
            "type" => "text",
            "text" => "16"
        ]
    ]
];

$whatsapp = new WhatsAppNotification();
$whatsapp->send($data); // Send WhatsApp message via queue

// Ensure to run: php artisan queue:work or use Supervisor