Skip to content

Commit 1b522a1

Browse files
committed
minor #20357 [Messenger][Notifier] Add some examples to the new Desktop channel notifications (javiereguiluz)
This PR was merged into the 7.2 branch. Discussion ---------- [Messenger][Notifier] Add some examples to the new Desktop channel notifications Related to the blog post that we published today: https://symfony.com/blog/new-in-symfony-7-2-desktop-notifications Commits ------- e033e43 [Notifier] Add some examples to the new Desktop channel notifications
2 parents 4726300 + e033e43 commit 1b522a1

File tree

1 file changed

+61
-10
lines changed

1 file changed

+61
-10
lines changed

notifier.rst

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ The notifier component supports the following channels:
3333
services like Slack and Telegram;
3434
* :ref:`Email channel <notifier-email-channel>` integrates the :doc:`Symfony Mailer </mailer>`;
3535
* Browser channel uses :ref:`flash messages <flash-messages>`.
36-
* :ref:`Push channel <notifier-push-channel>` sends notifications to phones and browsers via push notifications.
37-
* :ref:`Desktop channel <notifier-desktop-channel>` displays desktop notifications on the same host machine.
36+
* :ref:`Push channel <notifier-push-channel>` sends notifications to phones and
37+
browsers via push notifications.
38+
* :ref:`Desktop channel <notifier-desktop-channel>` displays desktop notifications
39+
on the same host machine.
40+
41+
.. versionadded:: 7.2
42+
43+
The ``Desktop`` channel was introduced in Symfony 7.2.
3844

3945
.. _notifier-sms-channel:
4046

@@ -635,28 +641,33 @@ configure the ``texter_transports``:
635641
Desktop Channel
636642
~~~~~~~~~~~~~~~
637643

638-
The desktop channel is used to display desktop notifications on the same host machine using
639-
:class:`Symfony\\Component\\Notifier\\Texter` classes. Currently, Symfony
640-
is integrated with the following providers:
644+
The desktop channel is used to display local desktop notifications on the same
645+
host machine using :class:`Symfony\\Component\\Notifier\\Texter` classes. Currently,
646+
Symfony is integrated with the following providers:
641647

642648
=============== ==================================== ==============================================================================
643649
Provider Package DSN
644650
=============== ==================================== ==============================================================================
645651
`JoliNotif`_ ``symfony/joli-notif-notifier`` ``jolinotif://default``
646652
=============== ==================================== ==============================================================================
647653

648-
.. versionadded: 7.2
654+
.. versionadded:: 7.2
649655

650656
The JoliNotif bridge was introduced in Symfony 7.2.
651657

652-
To enable a texter, add the correct DSN in your ``.env`` file and
653-
configure the ``texter_transports``:
658+
If you are using :ref:`Symfony Flex <symfony-flex>`, installing that package will
659+
also create the necessary environment variable and configuration. Otherwise, you'll
660+
need to add the following manually:
661+
662+
1) Add the correct DSN in your ``.env`` file:
654663

655664
.. code-block:: bash
656665
657666
# .env
658667
JOLINOTIF=jolinotif://default
659668
669+
2) Update the Notifier configuration to add a new texter transport:
670+
660671
.. configuration-block::
661672

662673
.. code-block:: yaml
@@ -699,9 +710,49 @@ configure the ``texter_transports``:
699710
;
700711
};
701712
702-
.. versionadded:: 7.2
713+
Now you can send notifications to your desktop as follows::
703714

704-
The ``Desktop`` channel was introduced in Symfony 7.2.
715+
// src/Notifier/SomeService.php
716+
use Symfony\Component\Notifier\Message\DesktopMessage;
717+
use Symfony\Component\Notifier\TexterInterface;
718+
// ...
719+
720+
class SomeService
721+
{
722+
public function __construct(
723+
private TexterInterface $texter,
724+
) {
725+
}
726+
727+
public function notifyNewSubscriber(User $user): void
728+
{
729+
$message = new DesktopMessage(
730+
'New subscription! 🎉',
731+
sprintf('%s is a new subscriber', $user->getFullName())
732+
);
733+
734+
$texter->send($message);
735+
}
736+
}
737+
738+
These notifications can be customized further, and depending on your operating system,
739+
they may support features like custom sounds, icons, and more::
740+
741+
use Symfony\Component\Notifier\Bridge\JoliNotif\JoliNotifOptions;
742+
// ...
743+
744+
$options = (new JoliNotifOptions())
745+
->setIconPath('/path/to/icons/error.png')
746+
->setExtraOption('sound', 'sosumi')
747+
->setExtraOption('url', 'https://example.com');
748+
749+
$message = new DesktopMessage('Production is down', <<<CONTENT
750+
❌ Server prod-1 down
751+
❌ Server prod-2 down
752+
✅ Network is up
753+
CONTENT, $options);
754+
755+
$texter->send($message);
705756

706757
Configure to use Failover or Round-Robin Transports
707758
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)