Desktop notification in PHP
Section intitulée the-need-for-desktop-notificationsThe need for desktop notifications
We celebrate this year the 20th birthday of PHP. Even if the language was originally created as a templating engine to generate web page, it’s more and more often used in the console to:
- fetch and install your dependencies;
- run your unit tests;
- do continuous integration;
- do continuous deployment;
- etc.
If your script takes some time to process, you will likely minimize the terminal and forget about it. Wouldn’t be cool if you could be notified by your desktop when the task just finished?
Each OS has its owns softwares to display a desktop notification. MacOS can provide access to its notification center with either the terminal-notifier
binary, via AppleScript
or with the growlnotify
binary, if Growl is installed. Most Linux distributions have the package libnotify-bin
preinstalled which makes available the executable notify-send
. For Windows, there is two applications: Notifu
for Windows 7 and Toaster
for Windows 8 and higher.
Each of those softwares has its own syntax and options. It’s not that easy to make your notification cross-platform. Introducing JoliNotif.
Section intitulée jolinotif-the-missing-php-notifierJoliNotif, the missing PHP notifier
JoliNotif is a tiny PHP library that allows you to display desktop notifications, whatever the OS you’re running:
JoliNotif looks for which notifiers are available on your current system and will use the best one to display your notification. In order to achieve that, it provides a factory that will return the appropriate notifier:
use Joli\JoliNotif\NotifierFactory;
/** @var \Joli\JoliNotif\Notifier $notifier */
$notifier = NotifierFactory::create();
All you have to do is create your notification then pass it to the Notifier:
use Joli\JoliNotif\Notification;
if ($notifier) {
$notification =
(new Notification())
->setBody('The notification body')
->setTitle('The notification title')
->setIcon(__DIR__.'/Resources/icons/success.png');
;
$notifier->send($notification);
}
Quite simple, isn’t it? :)
Section intitulée projects-using-jolinotifProjects using JoliNotif
Several projects already take advantage of JoliNotif. The first one was JoliCi. This Continuous Integration client powered by Docker allows you to run your Travis-CI builds locally. If you call the run
command with the --notify
option, it will use JoliNotif to inform you of the builds result when they finish.
Sismo is a Continuous Testing Server whose unique goal is to simply run your test suite and provide immediate feedback before you push your modifications on the server. You can configure Sismo with JoliNotif as the notifier to use for displaying a notification with the test suite result.
In some of your projects, you may have a large PHPUnit test suite that takes some time to run. So Mathieu Darse wrote phpunit-notifier, a PHPUnit listener that allows PHPUnit to inform you of the results of your test suite. You just have to configure the listener in your phpunit.xml
and you’re good to go.
Composer is often criticized because of its slowness. It’s even slower when there is a lot of scripts that run on post install or post update event (like in a typical symfony project). So, I wrote a simple composer plugin, composer-notifier, that will notify you when Composer finishes its commands.
You know some projects that could benefit from using JoliNotif? You need another notifier on your system? Let us know and contribute!
Commentaires et discussions
Ces clients ont profité de notre expertise
À l’occasion de la 12e édition du concours Europan Europe, JoliCode a conçu la plateforme technique du concours. Ce site permet la présentation des différents sites pour lesquels il y a un appel à projets, et encadre le processus de recueil des projets soumis par des milliers d’architectes candidats. L’application gère également toute la partie post-concours…
Dans le cadre d’une refonte complète de son architecture Web, le journal en ligne Mediapart a sollicité l’expertise de JoliCode afin d’accompagner ses équipes. Mediapart.fr est un des rares journaux 100% en ligne qui n’appartient qu’à ses lecteurs qui amène un fort traffic authentifiés et donc difficilement cachable. Pour effectuer cette migration, …
Nous avons entrepris une refonte complète du site, initialement développé sur Drupal, dans le but de le consolider et de jeter les bases d’un avenir solide en adoptant Symfony. La plateforme est hautement sophistiquée et propose une pléthore de fonctionnalités, telles que la gestion des abonnements avec Stripe et Paypal, une API pour l’application…