mehr-als-nix/notifier

Desktop Notifier Library.

v2.0.0-beta.3 2015-08-17 21:53 UTC

This package is auto-updated.

Last update: 2024-09-29 04:25:37 UTC


README

Build Status Code Climate Test Coverage Dependency Status HHVM Status

Desktop Notifications

Notifier acts as a wrapper for desktop notify applications on different operating systems.

Following notify wrappers are build in and would make checks to chose one of:

  • notify-send (Linux)
  • terminal-notifier (Mac)
  • toast.exe (Windows) nels-o/toaster

Install via composer

Add a dependency on mehr-als-nix/notifier to your project's composer.json file.

Here is a minimal example of a manually created composer.json file that just defines a dependency on mehr-als-nix/notifier

{
    "require": {
        "mehr-als-nix/notifier": "~2"
    }
}

For PHP environments prior to version 5.6 use instead:

{
    "require": {
        "mehr-als-nix/notifier": "~1"
    }
}

Usage

Example:

   \MehrAlsNix\Notifier\Notify::getInstance()
       ->sendMessage('Notification', 'This is a desktop message!');

Extend

Custom class has to extend from \MehrAlsNix\Notifier\Notification

<?php

namespace \Custom\Notifier;

class GrowlNotifier extends \MehrAlsNix\Notifier\Notification
{
    /**
     * Notify with `growlnotify`.
     *
     * @param string $title
     * @param string $message
     * @param string $icon    optional
     *
     * @return void
     */
    protected function notify($title, $message, $icon = null)
    {
        $icon = is_string($icon) ? $icon : $this->icon;
        $this->execute("growlnotify -t '{$title}' -m '{$message}' --image '{$icon}'");
    }

    /**
     * @inheritdoc
     *
     * @return bool
     */
    public function isAvailable()
    {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            return (bool) $this->execute('where.exe growlnotify');
        }
        
        return (bool) $this->execute('which growlnotify');
    }
}

And can then be used like:

    \MehrAlsNix\Notifier\Notify::getInstance('\Custom\Notifier\GrowlNotifier')
        ->sendMessage('Notification', 'This is a desktop message!');

or

    \MehrAlsNix\Notifier\Notify::getInstance('\Custom\Notifier\GrowlNotifier')
       ->setTitle('Notification')
       ->setMessage('This is a desktop message!')
       ->setIcon('/path/to/icon.png')
       ->send();