codewiser/laravel-notifications

Laravel 通知辅助函数

v1.1.9 2024-09-16 13:37 UTC

README

提供 Laravel 通知辅助函数。

它支持四种类型的通知消息: mailtelegrambroadcastdatabase。所有这些都实现了一个合同,因此我们可以一次性构建所有这些消息。

Broadcastdatabase 消息具有统一的负载格式: Web 通知。此格式已准备好在前端实施。

迁移

notifications.data 列迁移到 json 类型。

php artisan notifications:json
 
php artisan migrate

消息合同

所有消息—— mailtelegrambroadcastdatabase——实现 MessageContract,因此我们可以一次性构建消息。

use Codewiser\Notifications\Contracts\MessageContract;
use Codewiser\Notifications\Messages\MailMessage;
use Codewiser\Notifications\Messages\TelegramMessage;
use Codewiser\Notifications\Messages\BroadcastMessage;
use Codewiser\Notifications\Messages\DatabaseMessage;

class ReviewArticle extends \Illuminate\Notifications\Notification
{
    protected function build(MessageContract $message)
    {
        $message
            ->subject('Article Review')
            ->line('You need to review article.')
            ->action('Review', url('/article', [
                'article' => $this->article->getKey()
            ]))
            // Format as blockquote
            ->quotation('Silent is gold');
    }
    
    public function toMail(): MailMessage
    {
        return (new MailMessage)
            ->tap(fn($message) => $this->build($message))
            // Markdown table
            ->table(fn(MarkdownTable $table) => $table
                ->row(['Title 1', 'Title 2'])
                ->row([':---', '---:'])
                ->row(['Text 1', 'Text 2'])
            );
    }
    
    public function toTelegram(): TelegramMessage
    {
        return (new TelegramMessage)
            ->tap(fn($message) => $this->build($message))
            // Do not render preview
            ->linkPreviewOptions(is_disabled: true)
            // Prevent message forwarding or saving
            ->protected()
            // Notification without a sound 
            ->silently();
    }
    
    public function toBroadcast(): BroadcastMessage
    {
        return (new BroadcastMessage)
            ->tap(fn($message) => $this->build($message))
            // Remove action button
            ->withoutAction()
            // Keep notification on screen until user closes it
            ->requireInteraction()
            // Icon to display on notification
            ->icon('https://example.com/icon.svg');
            // etc
    }
    
    public function toDatabase(): DatabaseMessage
    {
        return (new DatabaseMessage)
            ->tap(fn($message) => $this->build($message))
            // Use level to order database notifications
            ->level('danger')
            // Create notification as already read
            ->silent();
    }
    
    public function toArray(): array
    {
        return $this->toDatabase()->toArray();
    }
}

广播消息

Broadcast 消息的负载格式为 Web 通知

数据库消息

Database 消息(作为 broadcast)具有 Web 通知 负载。

持久通知和提及

Database 通知可以标记为持久。您的应用程序可以限制用户尝试将此类通知标记为已读。然后在用户达到目标时将其标记为已读。

Database 通知可以绑定到一个模型,这样您就可以找到提到模型的通知。

例如,通知邀请用户评论某篇文章。在用户评论文章之前,通知保持未读状态。然后文章被评论,通知就不再相关了。

use Codewiser\Notifications\Messages\DatabaseMessage;
use Codewiser\Notifications\Models\DatabaseNotification;
use Codewiser\Notifications\Builders\NotificationBuilder;

// Notification
class ReviewArticleNotification extends \Illuminate\Notifications\Notification
{
    public function toDatabase(): DatabaseMessage
    {
        return (new DatabaseMessage)
            ->subject('Review article')
            ->action('Review', route('article.show', $this->article))
            ->persistent('You must review the article')
            ->bindTo($this->article);
    }
}

// Get unread notifications about an article
$article->mentions()
    ->where(fn (NotificationBuilder $builder) => $builder
        ->whereNotifiable($user)
        ->whereUnread());

// Later...
if ($article->wasReviewed()) {
    $user->notifications()
        ->whereType(ReviewArticleNotification::class)
        ->whereMentioned($article)
        ->whereUnread()
        ->markAsRead();
}

要启用绑定,为 mentions 表创建迁移。

php artisan notifications:mentions
 
php artisan migrate

Mentioned 合同和 HasMentions 特性添加到每个可能被提及的模型

use \Codewiser\Notifications\Contracts\Mentioned;
use \Codewiser\Notifications\Traits\HasMentions;
use \Illuminate\Database\Eloquent\Model;

class Article extends Model implements Mentioned
{
    // Provides mentions relation
    use HasMentions;
}

预览通知

您不仅可以预览邮件通知,还可以以相同的方式预览 Telegram 和广播通知。