deegitalbe/laravel-trustup-io-notifier

这是我创建的包:laravel-trustup-io-notifier


README

Trustup Pro Notifier

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

通过集中化的通知器实例轻松发送通知。支持通过自定义通知通道发送电子邮件(Postmark)、短信(Vonage/Nexmo)、邮政信件(Postbird)和推送(FCM)。只需一个应用名称和令牌即可使用所有这些通道。

安装

您可以通过composer安装此包

composer require deegitalbe/laravel-trustup-io-notifier

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="laravel-trustup-io-notifier-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel-trustup-io-notifier-config"

这是已发布配置文件的内容

return [

    /**
     * Sets at which URL the notifier is accessible.
     * Default value: https://notifier.trustup.io
     */
    'url' => env('TPN_URL', 'https://notifier.trustup.io'),

    /**
     * App name (default, invoicing...).
     */
    'app' => env('TPN_APP_NAME', 'default'),

    /**
     * App key
     */
    'key' => env('TPN_APP_KEY')
];

用法

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class DemoNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $channels;

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['tpn_sms', 'tpn_email', 'tpn_letter', 'tpn_push', 'database'];
    }
    
    public function toTPNSMS($notifiable): array
    {
        return [
            'to' => '31657616867',
            'text' => 'This is a test SMS'
        ];
    }
    
    public function toTPNEmail($notifiable): array
    {
        return [
            'to' => 'florian.husquinet@deegital.be',
            'html' => '<h1>Demo notification</h1><p>This is my email content...</p>',
            'plain' => 'This is my email content...',
            'subject' => 'Demo notification',
            'headers' => [
                'X-Model-ID' => 1,
                'X-Model-Type' => 'user'
            ]
        ];
    }
    
    public function toTPNPush($notifiable): array
    {
        return [
            'to' => 'f0TAY_cMSpi2nNnakQ_B0w:APA91bFV1-2sblQS1h9mGn6I_aYJEZtww67fCJXN3Ir7V1179q7z_oHDLipQL1KAFs7meUyveVomzi2wLHTYuzXR7rDDnFiOBmCzGFEx-_aRszH0B2lIIelqzBjEjo5cL2t98bZc3B5g',
            'name' => 'demo-notification',
            'title' => 'Demo notification',
            'body' => 'Lorem ipsum...',
            'data' => [
                'type' => 'new-request',
                'id' => '555'
            ]
        ];
    }
    
    public function toTPNLetter($notifiable): array
    {
        return [
            'name' => 'demo-notification',
            'pdf' => 'https://trustup-dev-billing.ams3.digitaloceanspaces.com/202202-2263%20(5).pdf'
        ];
    }
    
    public function toArray($notifiable)
    {
        return [
            'channels' => $this->via($notifiable),
            'to' => '31657616867',
            'text' => 'This is a demo notification'
        ];
    }
}

发送通知时会发生什么

每个提供的通知通道将通过API调用通知器实例,并生成一个uuid,然后存储在notification_log_uuids中。然后,您可以通过查询该uuid来查看您的通知在被发送到通知器后是如何表现的。
应从您的应用程序中排队发送通知,因为这是最佳做法。无论您在自己的应用程序中做什么,通知器都将接受通知并将其发送到自己的队列。处理之前可能存在几秒的延迟。