notify-eu/notify

Notify 通知驱动程序,适用于 Laravel

v1.0 2020-02-12 09:25 UTC

This package is auto-updated.

Last update: 2024-09-12 20:59:56 UTC


README

Latest Version on Packagist Software License Build Status StyleCI Total Downloads

本包使用 Notify 服务,通过 Laravel 5.7+ & 6.x 容易发送通知

内容

安装

您可以通过 composer 安装此包

$ composer require notify-eu/notify

设置 Notify 账户

将您的 ClientId、密钥和传输方式添加到 config/services.php

NOTIFY_URL 不是必须的。当您想要覆盖 Notify 调用的端点时可以使用它。(例如,用于预发布/生产的不同 URL)

// config/services.php
...
''notify' => [
         'clientID' => env('NOTIFY_CLIENT_ID'),
         'secret' => env('NOTIFY_SECRET'),
         'transport' => env('NOTIFY_TRANSPORT'),
         'url' => env('NOTIFY_URL')
],
...

将您的 Notify 凭据添加到 .env

// .env
...
NOTIFY_CLIENT_ID=
NOTIFY_SECRET=
NOTIFY_TRANSPORT=
NOTIFY_URL=
],
...

使用

现在您可以在通知中的 via() 方法中使用该通道

use App\User;
use Illuminate\Notifications\Notification;
use NotifyEu\Notify\NotifyChannel;
use NotifyEu\Notify\NotifyMessage;

class InvoicePaid extends Notification
{
    const TYPE = 'buyerContractApproval';
    protected $user;
    private $cc = [];
    private $bcc = [];


    /**
     * InvoicePaid constructor.
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * @param $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [NotifyChannel::class];
    }

    /**
     * @param $notifiable
     * @return NotifyMessage
     */
    public function toNotify($notifiable)
    {
        return NotifyMessage::create()
            ->setNotificationType(self::TYPE)
            ->setTransport('mail')
            ->setLanguage('en')
            ->setParams($this->getParams())
            ->setCc($this->cc)
            ->setBcc($this->bcc);
    }

    /**
     * @return array
     */
    private function getParams()
    {
        return array('userToken' => $this->user->getRememberToken());
    }

    /**
     * @param array $cc
     * format: array(array('name' => 'John Doe', 'recipient' => 'john@doe.com')
     */
    public function addCc(array $cc)
    {
        $this->cc = $cc;
    }

    /**
     * @param array $bcc
     * format: array(array('name' => 'John Doe', 'recipient' => 'john@doe.com')
     */
    public function addBcc(array $bcc)
    {
        $this->bcc = $bcc;
    }

通知

请确保可通知模型具有以下方法

/**
 * Route notifications for the notify channel.
 *
 * @return string
 */
public function routeNotificationForNotify()
{
    return [
        'name' => $this->name,
        'recipient' => $this->email,
    ];
}

所有可用方法

  • notificationType(''):接受字符串值。
  • transport(''):接受字符串值。如果未设置,则将回退到 .env 文件中的 NOTIFY_TRANSPORT
  • language(''):接受字符串值。
  • params($array):接受键/值参数的数组。
  • Cc($array):接受具有 'name'/'recipient' 键的数组。
  • Bcc($array):接受具有 'name'/'recipient' 键的数组。

事件

以下事件由通知触发。默认情况下

  • Illuminate\Notifications\Events\NotificationSending
  • Illuminate\Notifications\Events\NotificationSent

此通道在 Notify 调用失败时触发一次

  • Illuminate\Notifications\Events\NotificationFailed

要监听这些事件,请在 app/Listeners 中创建事件监听器

namespace App\Listeners;
	
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use NotifyEu\Notify\NotifyChannel;
	
class NotificationFailedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Notification failed event handler
     *
     * @param  NotificationFailed  $event
     * @return void
     */
    public function handle(NotificationFailed $event)
    {
        // Handle fail event for Notify
        //
        if($event->channel == NotifyChannel::class) {
	            
            $logData = [
            	'notifiable'    => $event->notifiable->id,
            	'notification'  => get_class($event->notification),
            	'channel'       => $event->channel,
            	'data'      => $event->data
            	];
            	
            Log::error('Notification Failed', $logData);
         }
    }
}

然后在 app/Providers/EventServiceProvider.php 中注册监听器

...
protected $listen = [

	'Illuminate\Notifications\Events\NotificationFailed' => [
		'App\Listeners\NotificationFailedListener',
	],

	'Illuminate\Notifications\Events\NotificationSent' => [
		'App\Listeners\NotificationSentListener',
	],
];
...

变更日志

请参阅 CHANGELOG 获取更多信息。

测试

$ composer test

安全

如果您发现任何安全问题,请通过电子邮件 info@notify.eu 而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可

MIT 许可证(MIT)。请参阅 许可文件 获取更多信息。