New in Symfony 7.2: Desktop Notifications
October 29, 2024 • Published by Javier Eguiluz
Symfony 7.2 is backed by:
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:
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);
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.