UNPKG

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