New in Symfony 7.2: Desktop Notifications

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:

$ 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:

use SymfonyComponentNotifierMessageDesktopMessage;
use SymfonyComponentNotifierTexterInterface;
// …

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())
);

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

That’s all. You will now see these notifications 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.

use SymfonyComponentNotifierBridgeJoliNotifJoliNotifOptions;
// …

$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);

$texter->send($message);

Sponsor the Symfony project.

Symfony Blog

Read More

Latest News

PHP-Releases

PHP 8.4.1 released!

PHP 8.1.31 released!

PHP 8.3.14 released!

PHP 8.2.26 released!

Generated by Feedzy