node.jsを使用してクロスプラットフォームネイティブ通知を送信しますMACOSの通知センター、Linux用のnotify-osd / libnotify-bin 、Windows 8/10用のトースター、または以前のWindowsバージョン用のタスクバーバルーン。これらの要件が満たされていない場合、Growlが使用されます。電子でうまく機能します。
MacOS、Windows、Linuxに関するネイティブ通知を表示します。
const notifier = require ( 'node-notifier' ) ;
// String
notifier . notify ( 'Message' ) ;
// Object
notifier . notify ( {
title : 'My notification' ,
message : 'Hello, there!'
} ) ; notify-osdまたはlibnotify-binがインストールされています(ubuntuはデフォルトでこれを持つ必要があります)レポーターの選択については、ドキュメントとフローチャートを参照してください。
npm install --save node-notifierCLIは別々のプロジェクトに移動しました:https://github.com/mikaelbr/node-notifier-cli
レポーターフローチャートで定義されているクロスプラットフォームフォールバックを備えた標準使用。以下のすべてのオプションは、ほとんどのプラットフォームで何らかの形で機能します。
const notifier = require ( 'node-notifier' ) ;
const path = require ( 'path' ) ;
notifier . notify (
{
title : 'My awesome title' ,
message : 'Hello from node, Mr. User!' ,
icon : path . join ( __dirname , 'coulson.jpg' ) , // Absolute path (doesn't work on balloons)
sound : true , // Only Notification Center or Windows Toasters
wait : true // Wait with callback, until user action is taken against notification, does not apply to Windows Toasters as they always wait or notify-send as it does not support the wait option
} ,
function ( err , response , metadata ) {
// Response is response from notification
// Metadata contains activationType, activationAt, deliveredAt
}
) ;
notifier . on ( 'click' , function ( notifierObject , options , event ) {
// Triggers if `wait: true` and user clicks notification
} ) ;
notifier . on ( 'timeout' , function ( notifierObject , options ) {
// Triggers if `wait: true` and notification closes
} ) ;Super Fine-Grained Controlが必要な場合は、各レポーターを個別にカスタマイズして、さまざまなシステムの特定のオプションをチューニングできるようにします。
各レポーターのドキュメントについては、以下を参照してください。
例:
const NotificationCenter = require ( 'node-notifier/notifiers/notificationcenter' ) ;
new NotificationCenter ( options ) . notify ( ) ;
const NotifySend = require ( 'node-notifier/notifiers/notifysend' ) ;
new NotifySend ( options ) . notify ( ) ;
const WindowsToaster = require ( 'node-notifier/notifiers/toaster' ) ;
new WindowsToaster ( options ) . notify ( ) ;
const Growl = require ( 'node-notifier/notifiers/growl' ) ;
new Growl ( options ) . notify ( ) ;
const WindowsBalloon = require ( 'node-notifier/notifiers/balloon' ) ;
new WindowsBalloon ( options ) . notify ( ) ;または、複数の記者を使用している場合(または怠け者)。
// NOTE: Technically, this takes longer to require
const nn = require ( 'node-notifier' ) ;
new nn . NotificationCenter ( options ) . notify ( ) ;
new nn . NotifySend ( options ) . notify ( ) ;
new nn . WindowsToaster ( options ) . notify ( options ) ;
new nn . WindowsBalloon ( options ) . notify ( options ) ;
new nn . Growl ( options ) . notify ( options ) ; NotificationCenter terminal-notifierのセットアップ。
ネイティブ通知センターには、MacOSバージョン10.8以降が必要です。以前のバージョンがある場合、Growlはフォールバックになります。 Growlがインストールされていない場合、コールバックでエラーが返されます。
node-notifier terminal-notifierをラップするため、プロパティをnotifyメソッドに渡すだけで、 terminal-notifier何でもできます。
例えば:
terminal-notifierが-messageと言う場合、 {message: 'Foo'}を実行できますterminal-notifierが表示された場合、 -list ALL 、 {list: 'ALL'}を実行できます。通知はこのモジュールの主な焦点であるため、リストとアクティブ化は機能しますが、文書化されていません。
const NotificationCenter = require ( 'node-notifier' ) . NotificationCenter ;
var notifier = new NotificationCenter ( {
withFallback : false , // Use Growl Fallback if <= 10.8
customPath : undefined // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
} ) ;
notifier . notify (
{
title : undefined ,
subtitle : undefined ,
message : undefined ,
sound : false , // Case Sensitive string for location of sound file, or use one of macOS' native sounds (see below)
icon : 'Terminal Icon' , // Absolute Path to Triggering Icon
contentImage : undefined , // Absolute Path to Attached Image (Content Image)
open : undefined , // URL to open on Click
wait : false , // Wait for User Action against Notification or times out. Same as timeout = 5 seconds
// New in latest version. See `example/macInput.js` for usage
timeout : 5 , // Takes precedence over wait if both are defined.
closeLabel : undefined , // String. Label for cancel button
actions : undefined , // String | Array<String>. Action label or list of labels in case of dropdown
dropdownLabel : undefined , // String. Label to be used if multiple actions
reply : false // Boolean. If notification should take input. Value passed as third argument in callback and event emitter.
} ,
function ( error , response , metadata ) {
console . log ( response , metadata ) ;
}
) ;注: waitオプションはtimeout: 5 。これにより、タイムアウトが5秒間設定されます。通知を粘着性にしません!
バージョン6.0の時点では、アプリケーションが適切に閉じることを確認するために、 10のデフォルトのtimeoutセットがあります。 timeoutを削除し、即座に閉じる通知(アクションをサポートしない)を取得するには、 timeoutをfalseに設定します。 actionを使用している場合は、ユーザーが応答する時間があることを確認するために、 timeout高い価値に設定することをお勧めします。
例外: replyが定義されている場合は、 timeout高い値に設定するか、まったくゼロに設定することをお勧めします。
MacOS通知の場合: icon 、 contentImage 、およびすべての形式のreply / actionsには、macos 10.9が必要です。
サウンドは、 Basso 、 Blow 、 Bottle 、 Frog 、 Funk 、 Glass 、 Hero 、 Morse 、 Ping 、 Pop 、 Purr 、 Sosumi 、 Submarine 、 Tinkです。
sound単にtrueである場合、 Bottleが使用されます。
参照:
カスタムパスの明確化
customPath terminal-notifierのフォーク/カスタムバージョンのバイナリへの相対または絶対パスの値を取得します。
例: ./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier /terminal-notifier.app/contents/macos/terminal-notifier
スポットライトの説明
terminal-notifier.app 、 mac.noindexフォルダーに存在し、Spotlightがアプリのインデックスを作成しないようにします。
WindowsToaster注:ネイティブWindows 8の通知には、画像にはいくつかの制限があります。
これらの制限は、トースト通知システムによるものです。良いヒントは、 path.joinまたはpath.delimiterのようなものを使用して、パスをクロスプラットフォームに保つことです。
Mikaelbr/Gulp-Notify#90から(コメント)
System>通知とアクションに移動することで機能させることができます。 「トースト」アプリは、バナーを有効にする必要があります。 (「Toast」アプリをクリックして「表示通知バナー」を設定することで、バナーをアクティブにすることができます)
Windows 10 Fall Creators Update(バージョン1709)注:
Snoretoastは、ネイティブの窓トーストを取得するために使用されます!
デフォルトの動作は、基礎となるToasterアプリケートンをappIDとして持つことです。これは期待どおりに機能しますが、通知のテキストとしてSnoreToast示しています。
Fall Creatorsの更新により、Windows 10の通知は、有効なappIDが指定されている場合にのみ機能します。 appIDは、アプリのインストール中に登録された値とまったく同じでなければなりません。
アプリのインストール時に指定したappIDのレジストリを検索することにより、アプリのIDを見つけることができます。たとえば、リスフレームワークを使用する場合、 appID com.squirrel.your.appのようなものになります。
const WindowsToaster = require ( 'node-notifier' ) . WindowsToaster ;
var notifier = new WindowsToaster ( {
withFallback : false , // Fallback to Growl or Balloons?
customPath : undefined // Relative/Absolute path if you want to use your fork of SnoreToast.exe
} ) ;
notifier . notify (
{
title : undefined , // String. Required
message : undefined , // String. Required if remove is not defined
icon : undefined , // String. Absolute path to Icon
sound : false , // Bool | String (as defined by http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx)
id : undefined , // Number. ID to use for closing notification.
appID : undefined , // String. App.ID and app Name. Defaults to no value, causing SnoreToast text to be visible.
remove : undefined , // Number. Refer to previously created notification to close.
install : undefined // String (path, application, app id). Creates a shortcut <path> in the start menu which point to the executable <application>, appID used for the notifications.
} ,
function ( error , response ) {
console . log ( response ) ;
}
) ;Growl const Growl = require ( 'node-notifier' ) . Growl ;
var notifier = new Growl ( {
name : 'Growl Name Used' , // Defaults as 'Node'
host : 'localhost' ,
port : 23053
} ) ;
notifier . notify ( {
title : 'Foo' ,
message : 'Hello World' ,
icon : fs . readFileSync ( __dirname + '/coulson.jpg' ) ,
wait : false , // Wait for User Action against Notification
// and other growl options like sticky etc.
sticky : false ,
label : undefined ,
priority : undefined
} ) ;Growlyの使用については、詳細をご覧ください。
WindowsBalloon Windowsの以前のバージョンでは、タスクバーの風船が使用されます(フォールバックがアクティブになり、うなり声が実行されていない限り)。 Balloons Notifierは、 notifuと呼ばれる素晴らしいプロジェクトを使用しています。
const WindowsBalloon = require ( 'node-notifier' ) . WindowsBalloon ;
var notifier = new WindowsBalloon ( {
withFallback : false , // Try Windows Toast and Growl first?
customPath : undefined // Relative/Absolute path if you want to use your fork of notifu
} ) ;
notifier . notify (
{
title : undefined ,
message : undefined ,
sound : false , // true | false.
time : 5000 , // How long to show balloon in ms
wait : false , // Wait for User Action against Notification
type : 'info' // The notification type : info | warn | error
} ,
function ( error , response ) {
console . log ( response ) ;
}
) ;プロジェクトホームページで完全な使用法をご覧ください: notifu 。
NotifySend注: notify-send waitフラグをサポートしていません。
const NotifySend = require ( 'node-notifier' ) . NotifySend ;
var notifier = new NotifySend ( ) ;
notifier . notify ( {
title : 'Foo' ,
message : 'Hello World' ,
icon : __dirname + '/coulson.jpg' ,
wait : false , // Defaults no expire time set. If true expire time of 5 seconds is used
timeout : 10 , // Alias for expire-time, time etc. Time before notify-send expires. Defaults to 10 seconds.
// .. and other notify-send flags:
'app-name' : 'node-notifier' ,
urgency : undefined ,
category : undefined ,
hint : undefined
} ) ;マンページのフラグとオプションを参照してくださいnotify-send(1)
node-notifierオープンソースソフトウェアを通じて可能になります。 node-notifierが使用するすべてのモジュールに感謝します。
terminal-notifierSnoretoastnotifugrowlyAraxeusのこの問題を参照してください。
SnoreToastテキストWindowsセクションの「Windows 10 Fall Creators Update」のメモを参照してください。短い答え: appIDを更新します。
WSL2内に通知が表示されない場合は、Exe Vendorファイル(Snoretoast)の許可を変更する必要がある場合があります。詳細については、問題を参照してください
TMUXセッション内でnode-notifierを使用する場合、システムにハングを引き起こす可能性があります。これは、このコメントで説明されている手順に従うことで解決できます
ここにはさらに詳細があります#61(コメント)。
node-notifierの構成オブジェクトにアイコンを定義している場合でも、通知には小さな端末アイコンが表示されます(このドキュメントの上部の例を参照)。
これは、MacOSの通知が機能する方法です。彼らは常に、通知を開始する親アプリケーションのアイコンを表示します。 node-notifierの場合、 terminal-notifierはイニシエーターであり、端子アイコンがアイコンとして定義されています。
カスタムアイコンを定義するには、 terminal-notifierをフォークし、アイコンを使用してカスタムバージョンを作成する必要があります。
詳細については、第71号を参照してください#71。
asarとして電子アプリをパッケージ化すると、 node-notifierがロードに失敗することがわかります。
ASARの仕組みにより、 asar内からバイナリを実行することはできません。簡単なソリューションとして、アプリをASARにパッケージ化する場合は、 node-notifierのvendor/フォルダー--unpackようにしてください。モジュールが通知バイナリにアクセスできるようにします。
次のコマンドでこれを行うことができます。
asar pack . app.asar --unpack " ./node_modules/node-notifier/vendor/** "または、ASARを直接使用せずにelectron-builderを使用している場合は、以下のようにbuild package.jsonを追加します。
...
build: {
asarUnpack: [
' ./node_modules/node-notifier/**/* ' ,
]
},
...PKGモジュールで使用する問題。この問題を確認してください:#220(コメント)
webpackの内部にnode-notifierを使用する場合、以下のスニペットをwebpack.config.jsに追加する必要があります。
node-notifierバイナリから通知機をロードするため、これは必要です。そのため、相対ファイルパスが必要です。 Webpackがモジュールをコンパイルすると、ファイルディレクトリを抑制し、特定のプラットフォームでnode-notifierがエラーを発生させます。
これを修正するには、Webpackを構成して、相対ファイルディレクトリを保持できます。次のコードをwebpack.config.jsに追加してそうしてください:
node: {
__filename : true ,
__dirname : true
} このパッケージは、MITライセンスを使用してライセンスされています。
SnoreToastとNotifuは、MITライセンス、LGPL-3、BSD 3節と一致しないベンダーバージョンにライセンスを備えています。私たちは弁護士ではありませんが、選択したライセンスを使用してこのパッケージをリリースしながら、これらのライセンスの条件に準拠するために最善の努力をしました。