UNPKG

17.2 kBMarkdownView Raw
1# node-notifier [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
2
3Send cross platform native notifications using Node.js. Notification Center for macOS,
4`notify-osd`/`libnotify-bin` for Linux, Toasters for Windows 8/10, or taskbar balloons for
5earlier Windows versions. Growl is used if none of these requirements are met.
6[Works well with Electron](#within-electron-packaging).
7
8![macOS Screenshot](https://raw.githubusercontent.com/mikaelbr/node-notifier/master/example/mac.png)
9![Native Windows Screenshot](https://raw.githubusercontent.com/mikaelbr/node-notifier/master/example/windows.png)
10
11## Input Example macOS Notification Center
12
13![Input Example](https://raw.githubusercontent.com/mikaelbr/node-notifier/master/example/input-example.gif)
14
15## Actions Example Windows SnoreToast
16
17![Actions Example](https://raw.githubusercontent.com/mikaelbr/node-notifier/master/example/windows-actions-example.gif)
18
19## Quick Usage
20
21Show a native notification on macOS, Windows, Linux:
22
23```javascript
24const notifier = require('node-notifier');
25// String
26notifier.notify('Message');
27
28// Object
29notifier.notify({
30 title: 'My notification',
31 message: 'Hello, there!'
32});
33```
34
35## Requirements
36
37- **macOS**: >= 10.8 for native notifications, or Growl if earlier.
38- **Linux**: `notify-osd` or `libnotify-bin` installed (Ubuntu should have this by default)
39- **Windows**: >= 8, or task bar balloons for Windows < 8. Growl as fallback. Growl takes precedence over Windows balloons.
40- **General Fallback**: Growl
41
42See [documentation and flow chart for reporter choice](./DECISION_FLOW.md).
43
44## Install
45
46```shell
47npm install --save node-notifier
48```
49
50## <abbr title="Command Line Interface">CLI</abbr>
51
52<abbr title="Command Line Interface">CLI</abbr> has moved to separate project:
53<https://github.com/mikaelbr/node-notifier-cli>
54
55## Cross-Platform Advanced Usage
56
57Standard usage, with cross-platform fallbacks as defined in the
58[reporter flow chart](./DECISION_FLOW.md). All of the options
59below will work in some way or another on most platforms.
60
61```javascript
62const notifier = require('node-notifier');
63const path = require('path');
64
65notifier.notify(
66 {
67 title: 'My awesome title',
68 message: 'Hello from node, Mr. User!',
69 icon: path.join(__dirname, 'coulson.jpg'), // Absolute path (doesn't work on balloons)
70 sound: true, // Only Notification Center or Windows Toasters
71 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
72 },
73 function (err, response, metadata) {
74 // Response is response from notification
75 // Metadata contains activationType, activationAt, deliveredAt
76 }
77);
78
79notifier.on('click', function (notifierObject, options, event) {
80 // Triggers if `wait: true` and user clicks notification
81});
82
83notifier.on('timeout', function (notifierObject, options) {
84 // Triggers if `wait: true` and notification closes
85});
86```
87
88If you want super fine-grained control, you can customize each reporter individually,
89allowing you to tune specific options for different systems.
90
91See below for documentation on each reporter.
92
93**Example:**
94
95```javascript
96const NotificationCenter = require('node-notifier/notifiers/notificationcenter');
97new NotificationCenter(options).notify();
98
99const NotifySend = require('node-notifier/notifiers/notifysend');
100new NotifySend(options).notify();
101
102const WindowsToaster = require('node-notifier/notifiers/toaster');
103new WindowsToaster(options).notify();
104
105const Growl = require('node-notifier/notifiers/growl');
106new Growl(options).notify();
107
108const WindowsBalloon = require('node-notifier/notifiers/balloon');
109new WindowsBalloon(options).notify();
110```
111
112Or, if you are using several reporters (or you're lazy):
113
114```javascript
115// NOTE: Technically, this takes longer to require
116const nn = require('node-notifier');
117
118new nn.NotificationCenter(options).notify();
119new nn.NotifySend(options).notify();
120new nn.WindowsToaster(options).notify(options);
121new nn.WindowsBalloon(options).notify(options);
122new nn.Growl(options).notify(options);
123```
124
125## Contents
126
127- [Notification Center documentation](#usage-notificationcenter)
128- [Windows Toaster documentation](#usage-windowstoaster)
129- [Windows Balloon documentation](#usage-windowsballoon)
130- [Growl documentation](#usage-growl)
131- [Notify-send documentation](#usage-notifysend)
132
133### Usage: `NotificationCenter`
134
135Same usage and parameter setup as [**`terminal-notifier`**](https://github.com/julienXX/terminal-notifier).
136
137Native Notification Center requires macOS version 10.8 or higher. If you have
138an earlier version, Growl will be the fallback. If Growl isn't installed, an
139error will be returned in the callback.
140
141#### Example
142
143Because `node-notifier` wraps around [**`terminal-notifier`**](https://github.com/julienXX/terminal-notifier),
144you can do anything `terminal-notifier` can, just by passing properties to the `notify`
145method.
146
147For example:
148
149- if `terminal-notifier` says `-message`, you can do `{message: 'Foo'}`
150- if `terminal-notifier` says `-list ALL`, you can do `{list: 'ALL'}`.
151
152Notification is the primary focus of this module, so listing and activating do work,
153but they aren't documented.
154
155### All notification options with their defaults:
156
157```javascript
158const NotificationCenter = require('node-notifier').NotificationCenter;
159
160var notifier = new NotificationCenter({
161 withFallback: false, // Use Growl Fallback if <= 10.8
162 customPath: undefined // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
163});
164
165notifier.notify(
166 {
167 title: undefined,
168 subtitle: undefined,
169 message: undefined,
170 sound: false, // Case Sensitive string for location of sound file, or use one of macOS' native sounds (see below)
171 icon: 'Terminal Icon', // Absolute Path to Triggering Icon
172 contentImage: undefined, // Absolute Path to Attached Image (Content Image)
173 open: undefined, // URL to open on Click
174 wait: false, // Wait for User Action against Notification or times out. Same as timeout = 5 seconds
175
176 // New in latest version. See `example/macInput.js` for usage
177 timeout: 5, // Takes precedence over wait if both are defined.
178 closeLabel: undefined, // String. Label for cancel button
179 actions: undefined, // String | Array<String>. Action label or list of labels in case of dropdown
180 dropdownLabel: undefined, // String. Label to be used if multiple actions
181 reply: false // Boolean. If notification should take input. Value passed as third argument in callback and event emitter.
182 },
183 function (error, response, metadata) {
184 console.log(response, metadata);
185 }
186);
187```
188
189---
190
191**Note:** The `wait` option is shorthand for `timeout: 5`. This just sets a timeout
192for 5 seconds. It does _not_ make the notification sticky!
193
194As of Version 6.0 there is a default `timeout` set of `10` to ensure that the application closes properly. In order to remove the `timeout` and have an instantly closing notification (does not support actions), set `timeout` to `false`. If you are using `action` it is recommended to set `timeout` to a high value to ensure the user has time to respond.
195
196_Exception:_ If `reply` is defined, it's recommended to set `timeout` to a either
197high value, or to nothing at all.
198
199---
200
201**For macOS notifications: `icon`, `contentImage`, and all forms of `reply`/`actions` require macOS 10.9.**
202
203Sound can be one of these: `Basso`, `Blow`, `Bottle`, `Frog`, `Funk`, `Glass`,
204`Hero`, `Morse`, `Ping`, `Pop`, `Purr`, `Sosumi`, `Submarine`, `Tink`.
205
206If `sound` is simply `true`, `Bottle` is used.
207
208---
209
210**See Also:**
211
212- [Example: specific Notification Centers](./example/advanced.js)
213- [Example: input](./example/macInput.js).
214
215---
216
217**Custom Path clarification**
218
219`customPath` takes a value of a relative or absolute path to the binary of your
220fork/custom version of **`terminal-notifier`**.
221
222**Example:** `./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier`
223
224**Spotlight clarification**
225
226`terminal-notifier.app` resides in a `mac.noindex` folder to prevent Spotlight from indexing the app.
227
228### Usage: `WindowsToaster`
229
230**Note:** There are some limitations for images in native Windows 8 notifications:
231
232- The image must be a PNG image
233- The image must be smaller than 1024×1024 px
234- The image must be less than 200kb
235- The image must be specified using an absolute path
236
237These limitations are due to the Toast notification system. A good tip is to use
238something like `path.join` or `path.delimiter` to keep your paths cross-platform.
239
240From [mikaelbr/gulp-notify#90 (comment)](https://github.com/mikaelbr/gulp-notify/issues/90#issuecomment-129333034)
241
242> You can make it work by going to System > Notifications & Actions. The 'toast'
243> app needs to have Banners enabled. (You can activate banners by clicking on the
244> 'toast' app and setting the 'Show notification banners' to On)
245
246---
247
248**Windows 10 Fall Creators Update (Version 1709) Note:**
249
250[**Snoretoast**](https://github.com/KDE/snoretoast) is used to get native Windows Toasts!
251
252The default behaviour is to have the underlying toaster applicaton as `appID`.
253This works as expected, but shows `SnoreToast` as text in the notification.
254
255With the Fall Creators Update, Notifications on Windows 10 will only work as
256expected if a valid `appID` is specified. Your `appID` must be exactly the same
257value that was registered during the installation of your app.
258
259You can find the ID of your App by searching the registry for the `appID` you
260specified at installation of your app. For example: If you use the squirrel
261framework, your `appID` will be something like `com.squirrel.your.app`.
262
263```javascript
264const WindowsToaster = require('node-notifier').WindowsToaster;
265
266var notifier = new WindowsToaster({
267 withFallback: false, // Fallback to Growl or Balloons?
268 customPath: undefined // Relative/Absolute path if you want to use your fork of SnoreToast.exe
269});
270
271notifier.notify(
272 {
273 title: undefined, // String. Required
274 message: undefined, // String. Required if remove is not defined
275 icon: undefined, // String. Absolute path to Icon
276 sound: false, // Bool | String (as defined by http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx)
277 id: undefined, // Number. ID to use for closing notification.
278 appID: undefined, // String. App.ID and app Name. Defaults to no value, causing SnoreToast text to be visible.
279 remove: undefined, // Number. Refer to previously created notification to close.
280 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.
281 },
282 function (error, response) {
283 console.log(response);
284 }
285);
286```
287
288### Usage: `Growl`
289
290```javascript
291const Growl = require('node-notifier').Growl;
292
293var notifier = new Growl({
294 name: 'Growl Name Used', // Defaults as 'Node'
295 host: 'localhost',
296 port: 23053
297});
298
299notifier.notify({
300 title: 'Foo',
301 message: 'Hello World',
302 icon: fs.readFileSync(__dirname + '/coulson.jpg'),
303 wait: false, // Wait for User Action against Notification
304
305 // and other growl options like sticky etc.
306 sticky: false,
307 label: undefined,
308 priority: undefined
309});
310```
311
312See more information about using [growly](https://github.com/theabraham/growly/).
313
314### Usage: `WindowsBalloon`
315
316For earlier versions of Windows, taskbar balloons are used (unless
317fallback is activated and Growl is running). The balloons notifier uses a great
318project called [**`notifu`**](http://www.paralint.com/projects/notifu/).
319
320```javascript
321const WindowsBalloon = require('node-notifier').WindowsBalloon;
322
323var notifier = new WindowsBalloon({
324 withFallback: false, // Try Windows Toast and Growl first?
325 customPath: undefined // Relative/Absolute path if you want to use your fork of notifu
326});
327
328notifier.notify(
329 {
330 title: undefined,
331 message: undefined,
332 sound: false, // true | false.
333 time: 5000, // How long to show balloon in ms
334 wait: false, // Wait for User Action against Notification
335 type: 'info' // The notification type : info | warn | error
336 },
337 function (error, response) {
338 console.log(response);
339 }
340);
341```
342
343See full usage on the [project homepage: **`notifu`**](http://www.paralint.com/projects/notifu/).
344
345### Usage: `NotifySend`
346
347**Note:** `notify-send` doesn't support the `wait` flag.
348
349```javascript
350const NotifySend = require('node-notifier').NotifySend;
351
352var notifier = new NotifySend();
353
354notifier.notify({
355 title: 'Foo',
356 message: 'Hello World',
357 icon: __dirname + '/coulson.jpg',
358
359 wait: false, // Defaults no expire time set. If true expire time of 5 seconds is used
360 timeout: 10, // Alias for expire-time, time etc. Time before notify-send expires. Defaults to 10 seconds.
361
362 // .. and other notify-send flags:
363 'app-name': 'node-notifier',
364 urgency: undefined,
365 category: undefined,
366 hint: undefined
367});
368```
369
370See flags and options on the man page [`notify-send(1)`](http://manpages.ubuntu.com/manpages/gutsy/man1/notify-send.1.html)
371
372## Thanks to OSS
373
374`node-notifier` is made possible through Open Source Software.
375A very special thanks to all the modules `node-notifier` uses.
376
377- [`terminal-notifier`](https://github.com/julienXX/terminal-notifier)
378- [`Snoretoast`](https://github.com/KDE/snoretoast)
379- [`notifu`](http://www.paralint.com/projects/notifu/)
380- [`growly`](https://github.com/theabraham/growly/)
381
382[![NPM downloads][npm-downloads]][npm-url]
383
384## Common Issues
385
386### Windows: `SnoreToast` text
387
388See note on "Windows 10 Fall Creators Update" in Windows section.
389_**Short answer:** update your `appID`._
390
391### Windows and WSL2
392
393If you don't see notifications within WSL2, you might have to change premission of exe vendor files (snoreToast).
394[See issue for more info](https://github.com/mikaelbr/node-notifier/issues/353)
395
396### Use inside tmux session
397
398When using `node-notifier` within a tmux session, it can cause a hang in the system.
399This can be solved by following the steps described in [this comment](https://github.com/julienXX/terminal-notifier/issues/115#issuecomment-104214742)
400
401There’s even more info [here](https://github.com/mikaelbr/node-notifier/issues/61#issuecomment-163560801)
402<https://github.com/mikaelbr/node-notifier/issues/61#issuecomment-163560801>.
403
404### macOS: Custom icon without Terminal icon
405
406Even if you define an icon in the configuration object for `node-notifier`, you will
407see a small Terminal icon in the notification (see the example at the top of this
408document).
409
410This is the way notifications on macOS work. They always show the icon of the
411parent application initiating the notification. For `node-notifier`, `terminal-notifier`
412is the initiator, and it has the Terminal icon defined as its icon.
413
414To define your custom icon, you need to fork `terminal-notifier` and build your
415custom version with your icon.
416
417See [Issue #71 for more info](https://github.com/mikaelbr/node-notifier/issues/71)
418<https://github.com/mikaelbr/node-notifier/issues/71>.
419
420### Within Electron Packaging
421
422If packaging your Electron app as an `asar`, you will find `node-notifier` will fail to load.
423
424Due to the way asar works, you cannot execute a binary from within an `asar`.
425As a simple solution, when packaging the app into an asar please make sure you
426`--unpack` the `vendor/` folder of `node-notifier`, so the module still has access to
427the notification binaries.
428
429You can do so with the following command:
430
431```bash
432asar pack . app.asar --unpack "./node_modules/node-notifier/vendor/**"
433```
434
435### Using with pkg
436
437For issues using with the pkg module. Check this issue out: https://github.com/mikaelbr/node-notifier/issues/220#issuecomment-425963752
438
439### Using Webpack
440
441When using `node-notifier` inside of `webpack`, you must add the snippet below to your `webpack.config.js`.
442
443This is necessary because `node-notifier` loads the notifiers from a binary, so it
444needs a relative file path. When webpack compiles the modules, it supresses file
445directories, causing `node-notifier` to error on certain platforms.
446
447To fix this, you can configure webpack to keep the relative file directories.
448Do so by append the following code to your `webpack.config.js`:
449
450```javascript
451node: {
452 __filename: true,
453 __dirname: true
454}
455```
456
457## License
458
459This package is licensed using the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
460
461[SnoreToast](https://raw.githubusercontent.com/mikaelbr/node-notifier/master/vendor/snoreToast/LICENSE) and [Notifu](https://raw.githubusercontent.com/mikaelbr/node-notifier/master/vendor/notifu/LICENSE) have licenses in their vendored versions which do not match the MIT license, LGPL-3 and BSD 3-Clause to be specific. We are not lawyers, but have made our best efforts to conform to the terms in those licenses while releasing this package using the license we chose.
462
463[npm-url]: https://npmjs.org/package/node-notifier
464[npm-image]: http://img.shields.io/npm/v/node-notifier.svg?style=flat
465[npm-downloads]: http://img.shields.io/npm/dm/node-notifier.svg?style=flat
466[travis-url]: http://travis-ci.org/mikaelbr/node-notifier
467[travis-image]: http://img.shields.io/travis/mikaelbr/node-notifier.svg?style=flat