Desktop notification in PHP

The 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.

JoliNotif, the missing PHP notifier

JoliNotif is a tiny PHP library that allows you to display desktop notifications, whatever the OS you’re running:

JoliNotif demo

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')


Quite simple, isn’t it? :)

Projects 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!

blog comments powered by Disqus