UNPKG

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