定制 pschocke/laravel-notification-settings 二次开发

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

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

pschocke/laravel-notification-settings

Composer 安装命令:

composer require pschocke/laravel-notification-settings

包简介

Let users decide on what event they want to be notified over which way

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This package tries to extend the default laravel notification system by giving models the ability to set individual notification settings for different ways of contact.

$model->saveNotificationSettingFromRequest($request);

$model->sendNotification(new OrderReceivedNotification($order); // this model will receive the notification on all channels he has configured

Installation

You can install the package via composer:

composer require pschocke/laravel-notification-settings

Publish and run the migration:

php artisan vendor:publish --provider="pschocke\NotificationSettings\NotificationSettingsServiceProvider" --tag="migrations"
php artisan migrate

Publish the config file:

php artisan vendor:publish --provider="pschocke\NotificationSettings\NotificationSettingsServiceProvider" --tag="config"

Usage

This package is driver based. By default, it can only send mail notifications. However, adding driver is a breeze.

To configure the package, you first need to publish the configuration file. It looks like this:

<?php

return [
    /**
     * The Settings the user can choose if he wants to be notified at a given event
     */
    'settings' => [

        /**
         * The default model. Add more if you want different settings for different models
         */
        'default' => [

            /**
             * The default events for this model. Add a new array with the notitication key to have different settings for different notification channels
             */
            'default' => [
                'event1' => 'description1',
                'event2' => 'description2',
            ],
        ],
    ],

    'verificationNotification' => pschocke\NotificationSettings\Notifications\NotificationSettingVerificationNotification::class,

    'handler' => [
        'mail' => pschocke\NotificationSettings\Handler\MailHandler::class,
    ],

    'driver' => [
        'mail' => [
            'verification' => [
                'enabled' => true,
                'method' => 'link',
            ],
        ],
    ],
];

If you want to configure different settings for different models, just add a setting in the array:

    'settings' => [
        App\Team::class => [
            'default' => [
                'mySetting' => 'description',
            ]
        ]
        'default' => [
            'default' => [
                'setting1' => 'description1',
                'setting2' => 'description2',
            ],
        ],
    ],

If you want to have different settings for different notification channels for a model, simply add an array with the notification key and the settings:

    'settings' => [
        App\Team::class => [
            'default' => [
                'mySetting' => 'description',
            ]
            'mail' => [
                'mailSetting' => 'mailDescription',
            ]
        ]
        'default' => [
            'default' => [
                'setting1' => 'description1',
                'setting2' => 'description2',
            ],
        ],
    ],

Prepare your model

All models that should have notification settings need to use the pschocke\NotificationSettings\HasNotificationSettings trait.

Add a new NotificationSetting to a model

To add a notification Channel to a model, just call the saveNotificationSetting() method on that model with the array of settings:

$myModel->saveNotificationSetting([
  'type' => 'mail',                 // the key of the handler
  'meta' => [                       // everything configured in the handler as required
      'email' => 'test@test.com',
  ],
  'settings' => [                   // the settings choices of the user
      'onNewLogin' => true,
      'onNewLogout' => false,
  ],
]);

Sending notifications

To send a notification to a model, just call the sendNotification() method on the model. The notification will be send through all channels that have the setting on true:

$myModel->sendNotification(new OrderShippedNotification($notification), 'orderShipped'); // the second parameter is the setting name.

Write your own notification Handler

Just sending notifications via email might be enough, but some times you want to send notifications via other channels too. Luckily, adding more handler is a breeze. Just create a new handler that implements the pschocke\NotificationSettings\Handler\HandlerInterface and extends the pschocke\NotificationSettings\Handler base class.

Just take a look at this handler that sends slack notifications:

namspace App\NotificationSettingHandler;

use pschocke\NotificationSettings\Models\NotificationSetting;
use pschocke\NotificationSettings\Handler\Handler;
use pschocke\NotificationSettings\Handler\HandlerInterface;

class SlackHandler extends Handler implements HandlerInterface
{

    protected $request = [
        'webhook' => ['required', 'url'],
    ]; // the request that needs to pass in order to save the model and enabled routing

    const via = 'slack'; // the channel name that is used in the notifications via method

    protected $notificationChannel = 'slack'; // the key that is used in the config file

    public function canSend(string $methodName): bool
    {
        return $methodName === 'routeNotificationForSlack';  // the method name to route the notification
    }

    public function getSend(NotificationSetting $notificationSetting) // returns what is needed to route the notification
    {
        return $notificationSetting->meta['webhook'];
    }
}

After adding the handler you just need to add it to the handlers array in your config file:

'handler' => [
    'mail' => pschocke\NotificationSettings\Handler\MailHandler::class,
    'slack' => App\NotificationSettingHandler\SlackHandler::class,
],

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email patrick@ausbildung-ms.de instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

pschocke/laravel-notification-settings 适用场景与选型建议

pschocke/laravel-notification-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「laravel notifications」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-28