mehr-als-nix/notifier

桌面通知库。

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

This package is auto-updated.

Last update: 2024-08-29 04:14:27 UTC


README

Build Status Code Climate Test Coverage Dependency Status HHVM Status

桌面通知

Notifier 充当不同操作系统桌面通知应用的包装器。

以下内置的通知包装器将会被构建并检查,以选择其中之一

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

通过 composer 安装

在项目的 composer.json 文件中添加对 mehr-als-nix/notifier 的依赖。

以下是一个手动创建的 composer.json 文件的最小示例,仅定义了对 mehr-als-nix/notifier 的依赖

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

对于版本 5.6 之前的 PHP 环境,请使用以下内容代替

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

使用方法

示例

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

扩展

自定义类必须从 \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');
    }
}

然后可以像这样使用

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

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