Ahmed Ghanem
Contributed by Ahmed Ghanem in #57683

The Symfony Notifier component allows you to notify users through channels like SMS messages, chat services, email messages, and push notifications on smartphones. In Symfony 7.2 we're adding a new desktop channel to send notifications to your local desktop.

This new channel uses the JoliNotif project internally, so you must first install it in your application:

1
$ composer require symfony/joli-notif-notifier

If you're using Symfony Flex, installing this package also creates the necessary environment variable in the .env file and updates the config/packages/notifier.yaml file. Now you're ready to send your first desktop notification. For example, you might use this in a side project to notify yourself whenever you get a new subscriber:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use Symfony\Component\Notifier\Message\DesktopMessage;
use Symfony\Component\Notifier\TexterInterface;
// ...

class SomeService
{
    public function __construct(
        private TexterInterface $texter,
    ) {
    }

    public function notifyNewSubscriber(User $user, Subscription $subscription): void
    {
        $message = new DesktopMessage(
            'New sale! 🎉',
            sprintf('New subscriber: %s (%s)', $user->getFullName(), $subscription->getPriceAsString())
        );

        $this->texter->send($message);
    }
}

That's all. This notification will now be sent to the desktop of the machine that it's running the code. So, when running it locally, you will see the following notification appear on your desktop:

Desktop Notification example in Symfony 7.2 Notifier

These notifications can be customized further, and depending on your operating system, they may support features like custom sounds, icons, and more.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\Component\Notifier\Bridge\JoliNotif\JoliNotifOptions;
// ...

$options = (new JoliNotifOptions())
    ->setIconPath('/path/to/icons/error.png')
    ->setExtraOption('sound', 'sosumi')
    ->setExtraOption('url', 'https://example.com');

$message = new DesktopMessage('Production is down', <<<CONTENT
    ❌ Server prod-1 down
    ❌ Server prod-2 down
    ✅ Network is up
    CONTENT, $options);

$this->texter->send($message);
Published in #Living on the edge