【node-notifier】node.jsでデスクトップ通知をする

表題の通り

デスクトップ通知が、簡単に実装できたのでメモしておく

f:id:taberu_salad:20190808124844p:plain

node-notifier

node-notifier
A Node.js module for sending notifications on native Mac, Windows (post and pre 8) and Linux (or Growl as fallback). Lat...

対応プラットフォーム

macOS: >= 10.8 for native notifications, or Growl if earlier.
Linux: notify-osd or libnotify-bin installed (Ubuntu should have this by default)
Windows: >= 8, or task bar balloons for Windows < 8. Growl as fallback. Growl takes precedence over Windows balloons.
General Fallback: Growl

インストール

特別なことは一切ないです。

$ npm install node-notifier

使い方

1. 最小のコードで通知を出す

まずは、最小構成で

const notifier = require('node-notifier');
notifier.notify({
'title': 'デスクトップ通知サンプル',
'message': 'Hello World!'
})

2. コールバック処理を書く

コールバック処理も問題なく書ける

const notifier = require('node-notifier');
const path = require('path');
notifier.notify({
title: 'コールバックサンプル',
message: 'Hello Callback!',
icon: path.join(__dirname, '/img/icon.jpg'),
sound: true,
wait: true
}, function (err, response) {
// コールバック関数書く
});
notifier.on('click', function (notifierObject, options) {
// wait: true の場合にクリックされた時のイベント処理
notifier.notify('クリックされました');
});
notifier.on('timeout', function (notifierObject, options) {
// wait: true の場合にタイムアウトした時のイベント処理
notifier.notify('タイムアウトしました');
});
タイトルとURLをコピーしました