byteblitz/notify

支持多渠道发送通知的软件包

v1.0.6 2024-02-11 21:17 UTC

This package is not auto-updated.

Last update: 2024-09-23 19:22:19 UTC


README

简介

Notify 是一个软件包,允许您为项目发送基于模板或自定义的多渠道通知。支持的渠道包括:邮件、FCM、短信、WhatsApp。

安装

您可以使用 composer 安装此软件包

$ composer require byteblitz/notify

然后,将服务提供者添加到 config/app.php 文件中。在 Laravel 5.5 及更高版本中,如果启用了软件包自动发现,可以跳过此步骤。

'providers' => [
    ...
    ByteBlitz\Notify\NotifyServiceProvider::class
    ...
];

您可以通过运行以下命令发布配置文件和资源

$ php artisan vendor:publish --provider="ByteBlitz\Notify\NotifyServiceProvider"

这将创建一个位于配置文件夹中的 notify.php 文件,并更新渠道详情和其他信息。

现在,我们已经将一些新文件发布到我们的应用程序中,我们需要通过以下命令重新加载它们

$ composer dump-autoload

现在,我们需要通过运行以下命令迁移我们的 notify_templates 和 notifications 表

$ php artisan migrate

基本

在控制器中使用此外观

use ByteBlitz\Notify\Facades\Notify;

让我们创建通知模板,您可以使用生成器来复制以下代码

$templates = [
  [
      'session'=>'New Registration To Admin',
      'receiver'=> 'admin',
      'slug' => 'new_registration_to_admin',
      'title' => 'New Registration',
      'mail_msg' => "<p>Hy Admin,</p><p>&nbsp; {name} is registered in ByteBlitz, Please review and verify. </p>",
      'notification_msg'=>"",
      'variables' => ['name'],
      'channels'=>['mail', 'fcm']
  ],
  [
      'session'=>'Registration success to customer',
      'receiver'=> 'customer',
      'slug' => 'registration_success_to_customer',
      'title' => 'Welcome to ByteBlitz',
      'notification_msg'=>"",
      'mail_msg' => "<p>Hy {name},</p><p>&nbsp;We'd like to welcome you to <strong>ByteBlitz</strong>, and thank you for joining us.</p>",
      'variables' => ['name'],
      'channels'=>['mail']
  ],
];

foreach($templates as $template) {
    Notify::createTemplate($template);
}

其他模板函数

//To get template
Notify::getTemplate('slug');
//To update template
Notify::updateTemplate($templateData, $id);
//To delete template
Notify::dropTemplate($id);
//To restore deleted template
Notify::restoreTemplate('slug');

现在,让我们使用创建的模板开始发送通知

首先,在所有守卫用户模型中使用此特性来接收通知

use ByteBlitz\Notify\Trait\NotifyBlitz;

使用模板发送通知,它将发送到我们在该模板上定义的所有渠道:注意:用户对象具有电子邮件和电话字段。

//variables need to be override so define template variables :
$variables = [
  'name'=>'Jhon'
];
$user->template('registration_success_to_customer', $variables)->notify();

如果您想将通知保存到数据库中,它将存储在 notifications 表中,请添加以下参数

$user->template('registration_success_to_customer', $variables, true, 'redirection route or url')->notify();

使用模板发送附件,我们可以发送多个附件。如果启用,它将被发送到邮件和 WhatsApp。

$user->template('registration_success_to_customer', $variables)->->attachments(['../attachment/path'])->notify();

如果您想使用自定义

//mailable class
$user
->customMail(new \App\Mail\CustomMail())
//custom to mail
->toMail('custom@gmail.com')
//custom phone number to send sms or whatsapp notification
->toPhone('+9198776*****')

//send custom messages
->via(['mail', 'fcm', 'whatsapp', 'sms'])
->title('Hellooooo')
->mailMsg('Mail Msg')
->message('notification message')
->notify();

模板用法

想要向邮件发送按钮,可以通过变量覆盖

notifyButton('https://#/', 'Create Account');

想要向邮件发送图片,可以通过变量覆盖

notifyImage('https://picperf.io/https://laravelnews.s3.amazonaws.com/images/laravel-featured.png');

配置

配置文件位于 config/notify.php,发布提供元素后。

请确保添加所有用户守卫以接收通知

'receivers'=>[
    'user'=>App\Models\User::class,
    'admin'=>App\Models\Admin::class
],

更改电子邮件模板标题、徽标或主颜色

'mail'=>[
    'title'=>'ByteBlitz',
    'logo'=>'',
    'primary_clr'=>'red'
],

更新渠道值或在 .env 文件中添加

//For firebase
'fcm'=> [
    'server_key'=>env('FCM_SERVER_KEY')
],

//For Whatsapp use ultramsg api provider
'whatsapp'=> [
    'ultramsg'=>[
        'instance_id'=> env('ULTRAMSG_INSTANCE_ID'),
        'token'=> env('ULTRAMSG_TOKEN'),
    ],
    'attachments_allowed'=>[
        'documents'=>['pdf'],
        'images'=>['png', 'jpg']
    ]
],

//For SMS, use twilio
'sms'=> [
    'twilio'=>[
        'sid'=>env('TWILIO_SID'),
        'auth_token'=>env('TWILIO_AUTH_TOKEN'),
        'from'=>env('TWILIO_FROM_NUMBER')
    ]
]

获取通知

获取用户收到的所有通知

$user->notifications();