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 Notification 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),
);
}
}
The constructor of Notification takes:
key- A unique key, such as
time..work On Absence category- A group of types, such as
time. The categorysecurity(constantCATEGORY_) marks types that a user cannot switch off.SECURITY events- The messages that trigger the type.
recipients- A description of the recipients in words, for the catalogue of types.
defaults- A
Channelwith the default for the channels in-app and e-mail.Choice channels- The supported channels,
['inby default.App', 'email'] 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. and
notification.; 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
rule, 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>
Links, channels and digest
A Notification turns a Notification (type and ID) into a page:
final readonly class AttendanceDayLink implements NotificationLink
{
public function subjectType(): string
{
return 'attendanceDay';
}
public function path(string $subjectId): string
{
return '/attendance-days/' . $subjectId;
}
}
A Notification adds a delivery channel next to in and email. A Digest adds a line to
the daily digest.
Live updates
Open pages receive live updates through Mercure. A package declares a topic with Realtime; 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();
}
}
template is the topic path with {name} placeholders. access lists the actors that may subscribe, as in
# (see Navigation and Access). grants decides for one user with the API's rules.
A listener queues an update with Realtime. 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]);
The update is sent after the command's transaction is committed; a rolled-back command sends nothing.