Native Browser Notifications
Send real-time, native browser notifications to everyone via Symfony's Notifier Component.
src/Controller/NotifierController.php
#[Route('/notify', name: 'app_notify')]
public function notify(Request $request, NotifierInterface $notifier): Response
{
$form = $this->createForm(SendNotificationForm::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message = SendNotificationForm::getTextChoices()[$form->getData()['message']];
// custom_mercure_chatter_transport is configured in notifier.yaml
$notification = new Notification($message, ['chat/custom_mercure_chatter_transport']);
$notifier->send($notification);
return $this->redirectToRoute('app_notify');
}
return $this->renderForm('notifier/notify.html.twig', ['form' => $form]);
}
templates/notifier/notify.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{{ stream_notifications(['/demo/notifier']) }}
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-primary">Send Notification</button>
{{ form_end(form) }}
{% endblock %}
UX Notify
$ composer require ux symfony/ux-notify
$ npm install --force
$ npm run watch