Skip to content

Sending notifications

You can send two types on notifications to the users.

Notification bars are displayed in specific situations as a message bar. They will be shown to whoever is doing a specific operation in the Back Office.

Example of an info notification

Flex Workflow notifications are sent to a specific user. They will appear in their profile in the Back Office.

Notification in profile

Display notification bars

You can have your PHP code send notification that will be displayed as a message bar in the Back Office. To do that, inject the NotificationHandlerInterface into your class. You can use one of the four methods representing the following notification types: info, success, warning and error.

1
$this->notificationHandler->info('Notification text');

To have the notification translated, make use of the TranslatorInterface. You need to provide the message strings in the translation files under the correct domain and key.

1
2
3
4
5
6
7
8
$this->notificationHandler->info(
    $this->translator->trans(
        /** @Desc("Notification text") */
        'example.notification.text',
        [],
        'domain'
    )
);

Create custom notifications using the Flex Workflow mechanism

Enterprise

You can send your own custom notifications to the user with the same mechanism that is used to send notification from Flex Workflow.

To create a new notification you must emit the NotificationSignal, which will be caught by the NotificationBundle:

1
2
3
4
5
$this->signalDispatcher->emit(new NotificationSignal([
    'ownerId' => $receiverId,  // User to be notified
    'type' => 'MyNotification:TypeName', // Notification type name
    'data' => $data, // Any data structure to be used by the notification (message etc.)
]));

To display the notification, write a Renderer and tag it as a service.

The example below presents a Renderer that uses Twig to render a view:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
declare(strict_types=1);

namespace AppBundle\Notification;

use EzSystems\Notification\SPI\Renderer\NotificationRenderer;
use EzSystems\Notification\SPI\Persistence\ValueObject\Notification;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

class MyRenderer implements NotificationRenderer
{
    protected $twig;
    protected $router;
    public function __construct(Environment $twig, RouterInterface $router)
    {
        $this->twig = $twig;
        $this->router = $router;
    }
    public function render(Notification $notification): string
    {
        return $this->twig->render('@FlexWorkflow/notification.html.twig', ['notification' => $notification]);
    }
    public function generateUrl(Notification $notification): ?string
    {
        if (property_exists($notification->data, 'content_id')) {
            return $this->router->generate('ez_content_draft_edit', [
                'contentId' => $notification->data->content_id,
                'versionNo' => $notification->data->version_number,
                'language' => $notification->data->language,
            ]);
        }
        return null;
    }
}

Finally, you need to add an entry to services.yml:

1
2
3
4
5
6
AppBundle\Notification\MyRenderer:
    arguments:
        - '@twig'
        - '@router'
    tags:
        - { name: ezstudio.notification.renderer, alias: 'MyNotification:TypeName' }
Read the Docs