You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

457 lines
15 KiB

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