moonshine-software / moonshine

Laravel Admin panel and more. Simple for beginners and powerful for experts. Using Blade, Alpine.js and Tailwind CSS.
https://moonshine-laravel.com
MIT License
718 stars 95 forks source link

feat: New Notification system #1156

Closed lee-to closed 1 month ago

lee-to commented 1 month ago

New notifications system

Now you can easily change the implementation of notifications. Just redefine in the provider

public function boot(): void
{
    parent::boot();

    $this->app->bind(MoonShineNotificationContract::class, TelegramNotification::class);
}

Implementation (Telegram notifications)

namespace App\MoonShine\Notifications;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use MoonShine\Laravel\Notifications\MoonShineNotificationContract;
use MoonShine\Support\Enums\Color;

final class TelegramNotification implements MoonShineNotificationContract
{
    public function notify(string $message, array $button = [], array $ids = [], string|Color|null $color = null): void
    {
        Http::post(
            'https://api.telegram.org/bot<TOKEn>/sendMessage',
            [
                'chat_id' => '<CHAT-ID>',
                'text' => $message,
            ]
        );
    }

    public function getAll(): Collection
    {
        return collect();
    }

    public function readAll(): void
    {

    }

    public function markAsRead(int|string $id): void
    {

    }

    public function getReadAllRoute(): string
    {
        return '';
    }
}

Decorator

namespace App\MoonShine\Notifications;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use MoonShine\Laravel\Notifications\MoonShineNotification;
use MoonShine\Laravel\Notifications\MoonShineNotificationContract;
use MoonShine\Support\Enums\Color;

final class TelegramNotificationDecorator implements MoonShineNotificationContract
{
    public function __construct(
        private MoonShineNotification $notification,
    )
    {
    }

    public function notify(string $message, array $button = [], array $ids = [], string|Color|null $color = null): void
    {
        $this->notification->notify($message, $button, $ids, $color);

        Http::post(
            'https://api.telegram.org/bot<TOKEn>/sendMessage',
            [
                'chat_id' => '<CHAT-ID>',
                'text' => $message,
            ]
        );
    }

    public function getAll(): Collection
    {
        return $this->notification->getAll();
    }

    public function readAll(): void
    {
        $this->notification->readAll();
    }

    public function markAsRead(int|string $id): void
    {
        $this->notification->markAsRead($id);
    }

    public function getReadAllRoute(): string
    {
        return $this->notification->getReadAllRoute();
    }
}