Desktop notifications
A user who starts a long run of a model can ask the browser to tell them when it is over (Settings → Notifications, see the user manual). A plugin that runs a model (a review, a generator, a translation) takes part in two steps: it registers a source, so the run is in the user's list, and it reports the end of a run.
Registering a source
@Autowired
private NotificationService notificationService;
// onExtensionLoad
notificationService.register(bundle.getSymbolicName(),
new NotificationService.Source("reviewer.review", () -> T.localized(REVIEW_THE_BOOK), false));
What a user chose stays in their settings when the plugin is not loaded and applies again when it is back; saving the tab does not drop it.
Reporting the end of a run
new DesktopNotifier(ui).finished("reviewer.review", false, book.getName()); // ended well
new DesktopNotifier(ui).failed("reviewer.review", false, book.getName()); // ended with an error
-
Call it where the UI is changed (inside
ui.access):DesktopNotifieris@Configurableand reads the settings of the current user. -
Do not call it for a run the user stopped.
-
automaticsays the run started by itself. Only the entry of that kind decides: a user who checked Name is not told about the automatic run, a user who checked Name (automatic) is not told about the one they opened. A source registered withhasAutomatic = falseignores the flag. -
detailis a second line (the title of the book),nullfor none. -
It never throws. Nothing is shown when notifications are off, the entry is not chosen, the source is not registered, the browser does not allow them, or the user is looking at the page.
A run that may end after the page that started it is gone (the tab was closed) can let the panel of the runs say it: declare the source
on its run of the registry, Runs.run(...).notifies("reviewer.review", false, book.getName()), and end the run with how it went,
run.end(Runs.Outcome.FINISHED) or FAILED. When the run's page is gone at its end, the panel (RunCorner) of one live page of the same
user sends the notification; a stopped run (STOPPED, also any run that was asked to stop()) sends nothing. The page that started it,
when it is still there, keeps calling DesktopNotifier itself as above: the panel stays quiet then. See
the runs.
How it works
NotificationService.prepare(source, automatic, outcome, detail) returns the Notice (title, body, tag) or null; the body is "<name> finished" /
"<name> failed" in the user's language. The tag is the key of the entry, so two notices of the same entry replace each other in the browser.
DesktopNotifier sends the notice with JavascriptLibrary.showDesktopNotification, which shows it only when the browser's
Notification.permission is granted, the page is hidden or has no focus, and the page is a secure context (HTTPS or localhost); on
browsers where new Notification is refused (phones) it goes through the service worker.
The choice is stored in NotificationSettings, a Setting row per user (@DiscriminatorValue, listed in persistence.xml, no migration) with the
extended attributes enabled and chosen (the keys of the entries: the source id, and id.auto for the automatic run).
The permission belongs to the browser and is not stored. The check box of the tab is the switch of the user (NotificationSettings.enabled, what
prepare checks) and only enables the buttons; the button Register this browser is watched by JavascriptLibrary.watchNotificationPermission, which
asks the browser from the click itself (browsers refuse the request otherwise) and sends the answer back as the event marginalia-notification-permission;
NotificationSettingsEditor shows the state and says why when the click did not get the permission. A browser that was not registered shows nothing, whatever
the user chose.
The sources of the application
The sources of the plugins
The ids are the key of the plugin plus the suffix. The name in the list starts with the plugin (Reviewer: generate a review).
The two translators (Conlang and Real language) run from commands and the command bar, which have no source.