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);
Perhaps I don't understand the use-case, but since the desktop notifications target the fpm/cli host, you will only receive notifications in a development environment.
@Martijn that's correct! In addition to sending notifications from the applications that you are developing, you could run on your machine some background applications/commands that do things or check things and notify you (directly to your desktop!) when something noticeable happens.
This feature is magic. I tried it and it's cool!