.. include:: /Includes.rst.txt .. _extending-events-notifications: ============================== Notifications and Live Updates ============================== A package tells people what its messages report: in the notification centre, by e-mail and as a live update in open pages. The messages are listed in :ref:`Events and Messages `. Notification types ================== A notification type implements ``NotificationType`` and names the messages that trigger it. This type of the attendance package informs HR about work on an absence day: .. code-block:: php /** * @implements NotificationType */ final readonly class WorkOnAbsenceNotification implements NotificationType { public function __construct( private Recipients $recipients, ) {} public static function definition(): NotificationTypeDefinition { return new NotificationTypeDefinition( 'time.workOnAbsence', 'time', [WorkOnAbsenceDetected::class], 'HR', new ChannelChoice(true, true), ); } public function notices(object $event): array { $companyId = CompanyId::fromString($event->companyId); $parameters = ['employeeName' => $this->recipients->displayName($event->employeeId) ?? '', 'date' => $event->date->format('Y-m-d')]; return \array_map( static fn(string $userId): Notice => new Notice($userId, $companyId, $parameters, new NotificationSubject('attendanceDay', $event->dayId)), $this->recipients->holders($companyId, Role::Hr), ); } } The constructor of ``NotificationTypeDefinition`` takes: ``key`` A unique key, such as ``time.workOnAbsence``. ``category`` A group of types, such as ``time``. The category ``security`` (constant ``CATEGORY_SECURITY``) marks types that a user cannot switch off. ``events`` The messages that trigger the type. ``recipients`` A description of the recipients in words, for the catalogue of types. ``defaults`` A ``ChannelChoice`` with the default for the channels in-app and e-mail. ``channels`` The supported channels, ``['inApp', 'email']`` by default. ``supportsDigest`` Whether the e-mails of the type can be summarised in the daily digest. ``notices()`` receives the message and returns a ``Notice`` per recipient: user ID, company, parameters and optionally the subject the notification is about. A notice must not contain more than the recipient may read through the API. Users choose per type whether they get it in-app and by e-mail. Texts of a notification ----------------------- Title and body are translations with the keys ``notification.type..title`` and ``notification.type..body``; the parameters of the notice are the placeholders (see :ref:`extending-translations-icons`). A parameter whose name ends in ``Text`` holds a translation key, such as ``ruleText: 'screen.violation.rule.dailyAverageExceeded'``, shown in the reader's language from any installed package that has it, or as the last part of the key: .. code-block:: xml notification.type.time.workOnAbsence.body {employeeName} worked on {date}, a day of approved absence. Links, channels and digest -------------------------- A ``NotificationLink`` turns a ``NotificationSubject`` (type and ID) into a page: .. code-block:: php final readonly class AttendanceDayLink implements NotificationLink { public function subjectType(): string { return 'attendanceDay'; } public function path(string $subjectId): string { return '/attendance-days/' . $subjectId; } } A ``NotificationChannel`` adds a delivery channel next to ``inApp`` and ``email``. A ``DigestSection`` adds a line to the daily digest. Live updates ============ Open pages receive live updates through Mercure. A package declares a topic with ``RealtimeTopic``; an unregistered topic is never granted. This is the topic of a user's approval inbox: .. code-block:: php final readonly class UserApprovalsTopic implements RealtimeTopic { public function template(): string { return '/users/{userId}/approvals'; } public function access(): array { return ['self', 'approver']; } public function grants(User $user, CompanyId $companyId, array $parameters): bool { return ($parameters['userId'] ?? null) === $user->getUserIdentifier(); } } ``template()`` is the topic path with ``{name}`` placeholders. ``access()`` lists the actors that may subscribe, as in ``#[Access]`` (see :ref:`extending-navigation-access`). ``grants()`` decides for one user with the API's rules. A listener queues an update with ``RealtimeUpdates``. The payload holds the kind, IDs and a short summary, never more than the API shows: .. code-block:: php $this->updates->queue('/users/' . $userId . '/approvals', ['kind' => 'approval.withdrawn', 'approvalId' => $id]); The update is sent after the command's transaction is committed; a rolled-back command sends nothing.