UNPKG

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