2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Symfony Component Advent Calendar 2024の23日目の記事です。

SymfonyBlogでDesktop Notificationsが紹介されていたので作ってみます。
https://symfony.com/blog/new-in-symfony-7-2-desktop-notifications

完成図

右側の通知がDesktop Notificationsです。
スクリーンショット 2024-12-23 16.00.40.png

手順

symfony new test-console --version="7.2.x-dev"
cd test-console/

追加でrequireするのはこれだけ!

composer require symfony/joli-notif-notifier
// src/Command/TestCommand.php

<?php

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Notifier\Message\DesktopMessage;
use Symfony\Component\Notifier\TexterInterface;

#[AsCommand(name: 'punpun:stop')]
class TestCommand extends Command
{
    public function __construct(private TexterInterface $texter)
    {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->text('リラックス、リラックス😌');
        $io->text('6秒間、深呼吸をしましょう。');
        $io->progressStart(6);
        $i = 0;
        while ($i++ < 6) {
            sleep(1);
            $io->progressAdvance();
        }
        $io->progressFinish();


        // Desktop Notificationsの部分 ここから ↓
        $message = new DesktopMessage(
            'Done!! いつもお疲れ様',
            'たまには自分にご褒美を🍺🍰🍦🍙🍣',
        );
        $this->texter->send($message);
        // Desktop Notificationsの部分 ここまで ↑

        return Command::SUCCESS;
    }
}

最後に

バックグラウンドで何か長めの処理をしている時に、
Desktop Notificationsで通知できるのは便利なので
機会があれば使ってみてください🥂

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?