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

/**
 * @implements NotificationType<WorkOnAbsenceDetected>
 */
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),
        );
    }
}
Copied!

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.<key>.title and notification.type.<key>.body; the parameters of the notice are the placeholders (see Translations and 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:

<unit id="notification.type.time.workOnAbsence.body" name="notification.type.time.workOnAbsence.body">
  <segment>
    <source>notification.type.time.workOnAbsence.body</source>
    <target>{employeeName} worked on {date}, a day of approved absence.</target>
  </segment>
</unit>
Copied!

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:

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();
    }
}
Copied!

template() is the topic path with {name} placeholders. access() lists the actors that may subscribe, as in #[Access] (see Navigation and 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:

$this->updates->queue('/users/' . $userId . '/approvals', ['kind' => 'approval.withdrawn', 'approvalId' => $id]);
Copied!

The update is sent after the command's transaction is committed; a rolled-back command sends nothing.