web 3d图形渲染器
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.

461 lines
16 KiB

  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. ## Actions Example Windows SnoreToast
  11. ![Actions Example](https://raw.githubusercontent.com/mikaelbr/node-notifier/master/example/windows-actions-example.gif)
  12. ## Quick Usage
  13. Show a native notification on macOS, Windows, Linux:
  14. ```javascript
  15. const notifier = require('node-notifier');
  16. // String
  17. notifier.notify('Message');
  18. // Object
  19. notifier.notify({
  20. title: 'My notification',
  21. message: 'Hello, there!'
  22. });
  23. ```
  24. ## Requirements
  25. - **macOS**: >= 10.8 for native notifications, or Growl if earlier.
  26. - **Linux**: `notify-osd` or `libnotify-bin` installed (Ubuntu should have this by default)
  27. - **Windows**: >= 8, or task bar balloons for Windows < 8. Growl as fallback. Growl takes precedence over Windows balloons.
  28. - **General Fallback**: Growl
  29. See [documentation and flow chart for reporter choice](./DECISION_FLOW.md).
  30. ## Install
  31. ```shell
  32. npm install --save node-notifier
  33. ```
  34. ## <abbr title="Command Line Interface">CLI</abbr>
  35. <abbr title="Command Line Interface">CLI</abbr> has moved to separate project:
  36. <https://github.com/mikaelbr/node-notifier-cli>
  37. ## Cross-Platform Advanced Usage
  38. Standard usage, with cross-platform fallbacks as defined in the
  39. [reporter flow chart](./DECISION_FLOW.md). All of the options
  40. below will work in some way or another on most platforms.
  41. ```javascript
  42. const notifier = require('node-notifier');
  43. const path = require('path');
  44. notifier.notify(
  45. {
  46. title: 'My awesome title',
  47. message: 'Hello from node, Mr. User!',
  48. icon: path.join(__dirname, 'coulson.jpg'), // Absolute path (doesn't work on balloons)
  49. sound: true, // Only Notification Center or Windows Toasters
  50. 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
  51. },
  52. function (err, response) {
  53. // Response is response from notification
  54. }
  55. );
  56. notifier.on('click', function (notifierObject, options, event) {
  57. // Triggers if `wait: true` and user clicks notification
  58. });
  59. notifier.on('timeout', function (notifierObject, options) {
  60. // Triggers if `wait: true` and notification closes
  61. });
  62. ```
  63. If you want super fine-grained control, you can customize each reporter individually,
  64. allowing you to tune specific options for different systems.
  65. See below for documentation on each reporter.
  66. **Example:**
  67. ```javascript
  68. const NotificationCenter = require('node-notifier/notifiers/notificationcenter');
  69. new NotificationCenter(options).notify();
  70. const NotifySend = require('node-notifier/notifiers/notifysend');
  71. new NotifySend(options).notify();
  72. const WindowsToaster = require('node-notifier/notifiers/toaster');
  73. new WindowsToaster(options).notify();
  74. const Growl = require('node-notifier/notifiers/growl');
  75. new Growl(options).notify();
  76. const WindowsBalloon = require('node-notifier/notifiers/balloon');
  77. new WindowsBalloon(options).notify();
  78. ```
  79. Or, if you are using several reporters (or you're lazy):
  80. ```javascript
  81. // NOTE: Technically, this takes longer to require
  82. const nn = require('node-notifier');
  83. new nn.NotificationCenter(options).notify();
  84. new nn.NotifySend(options).notify();
  85. new nn.WindowsToaster(options).notify(options);
  86. new nn.WindowsBalloon(options).notify(options);
  87. new nn.Growl(options).notify(options);
  88. ```
  89. ## Contents
  90. - [Notification Center documentation](#usage-notificationcenter)
  91. - [Windows Toaster documentation](#usage-windowstoaster)
  92. - [Windows Balloon documentation](#usage-windowsballoon)
  93. - [Growl documentation](#usage-growl)
  94. - [Notify-send documentation](#usage-notifysend)
  95. ### Usage: `NotificationCenter`
  96. Same usage and parameter setup as [**`terminal-notifier`**](https://github.com/julienXX/terminal-notifier).
  97. Native Notification Center requires macOS version 10.8 or higher. If you have
  98. an earlier version, Growl will be the fallback. If Growl isn't installed, an
  99. error will be returned in the callback.
  100. #### Example
  101. Because `node-notifier` wraps around [**`terminal-notifier`**](https://github.com/julienXX/terminal-notifier),
  102. you can do anything `terminal-notifier` can, just by passing properties to the `notify`
  103. method.
  104. For example:
  105. - if `terminal-notifier` says `-message`, you can do `{message: 'Foo'}`
  106. - if `terminal-notifier` says `-list ALL`, you can do `{list: 'ALL'}`.
  107. Notification is the primary focus of this module, so listing and activating do work,
  108. but they aren't documented.
  109. ### All notification options with their defaults:
  110. ```javascript
  111. const NotificationCenter = require('node-notifier').NotificationCenter;
  112. var notifier = new NotificationCenter({
  113. withFallback: false, // Use Growl Fallback if <= 10.8
  114. customPath: undefined // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
  115. });
  116. notifier.notify(
  117. {
  118. title: undefined,
  119. subtitle: undefined,
  120. message: undefined,
  121. sound: false, // Case Sensitive string for location of sound file, or use one of macOS' native sounds (see below)
  122. icon: 'Terminal Icon', // Absolute Path to Triggering Icon
  123. contentImage: undefined, // Absolute Path to Attached Image (Content Image)
  124. open: undefined, // URL to open on Click
  125. wait: false, // Wait for User Action against Notification or times out. Same as timeout = 5 seconds
  126. // New in latest version. See `example/macInput.js` for usage
  127. timeout: 5, // Takes precedence over wait if both are defined.
  128. closeLabel: undefined, // String. Label for cancel button
  129. actions: undefined, // String | Array<String>. Action label or list of labels in case of dropdown
  130. dropdownLabel: undefined, // String. Label to be used if multiple actions
  131. reply: false // Boolean. If notification should take input. Value passed as third argument in callback and event emitter.
  132. },
  133. function (error, response, metadata) {
  134. console.log(response, metadata);
  135. }
  136. );
  137. ```
  138. ---
  139. **Note:** The `wait` option is shorthand for `timeout: 5`. This just sets a timeout
  140. for 5 seconds. It does _not_ make the notification sticky!
  141. As 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.
  142. _Exception:_ If `reply` is defined, it's recommended to set `timeout` to a either
  143. high value, or to nothing at all.
  144. ---
  145. **For macOS notifications: `icon`, `contentImage`, and all forms of `reply`/`actions` require macOS 10.9.**
  146. Sound can be one of these: `Basso`, `Blow`, `Bottle`, `Frog`, `Funk`, `Glass`,
  147. `Hero`, `Morse`, `Ping`, `Pop`, `Purr`, `Sosumi`, `Submarine`, `Tink`.
  148. If `sound` is simply `true`, `Bottle` is used.
  149. ---
  150. **See Also:**
  151. - [Example: specific Notification Centers](./example/advanced.js)
  152. - [Example: input](./example/macInput.js).
  153. ---
  154. **Custom Path clarification**
  155. `customPath` takes a value of a relative or absolute path to the binary of your
  156. fork/custom version of **`terminal-notifier`**.
  157. **Example:** `./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier`
  158. **Spotlight clarification**
  159. `terminal-notifier.app` resides in a `mac.noindex` folder to prevent Spotlight from indexing the app.
  160. ### Usage: `WindowsToaster`
  161. **Note:** There are some limitations for images in native Windows 8 notifications:
  162. - The image must be a PNG image
  163. - The image must be smaller than 1024×1024 px
  164. - The image must be less than 200kb
  165. - The image must be specified using an absolute path
  166. These limitations are due to the Toast notification system. A good tip is to use
  167. something like `path.join` or `path.delimiter` to keep your paths cross-platform.
  168. From [mikaelbr/gulp-notify#90 (comment)](https://github.com/mikaelbr/gulp-notify/issues/90#issuecomment-129333034)
  169. > You can make it work by going to System > Notifications & Actions. The 'toast'
  170. > app needs to have Banners enabled. (You can activate banners by clicking on the
  171. > 'toast' app and setting the 'Show notification banners' to On)
  172. ---
  173. **Windows 10 Fall Creators Update (Version 1709) Note:**
  174. [**Snoretoast**](https://github.com/KDE/snoretoast) is used to get native Windows Toasts!
  175. The default behaviour is to have the underlying toaster applicaton as `appID`.
  176. This works as expected, but shows `SnoreToast` as text in the notification.
  177. With the Fall Creators Update, Notifications on Windows 10 will only work as
  178. expected if a valid `appID` is specified. Your `appID` must be exactly the same
  179. value that was registered during the installation of your app.
  180. You can find the ID of your App by searching the registry for the `appID` you
  181. specified at installation of your app. For example: If you use the squirrel
  182. framework, your `appID` will be something like `com.squirrel.your.app`.
  183. ```javascript
  184. const WindowsToaster = require('node-notifier').WindowsToaster;
  185. var notifier = new WindowsToaster({
  186. withFallback: false, // Fallback to Growl or Balloons?
  187. customPath: undefined // Relative/Absolute path if you want to use your fork of SnoreToast.exe
  188. });
  189. notifier.notify(
  190. {
  191. title: undefined, // String. Required
  192. message: undefined, // String. Required if remove is not defined
  193. icon: undefined, // String. Absolute path to Icon
  194. sound: false, // Bool | String (as defined by http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx)
  195. id: undefined, // Number. ID to use for closing notification.
  196. appID: undefined, // String. App.ID and app Name. Defaults to no value, causing SnoreToast text to be visible.
  197. remove: undefined, // Number. Refer to previously created notification to close.
  198. 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.
  199. },
  200. function (error, response) {
  201. console.log(response);
  202. }
  203. );
  204. ```
  205. ### Usage: `Growl`
  206. ```javascript
  207. const Growl = require('node-notifier').Growl;
  208. var notifier = new Growl({
  209. name: 'Growl Name Used', // Defaults as 'Node'
  210. host: 'localhost',
  211. port: 23053
  212. });
  213. notifier.notify({
  214. title: 'Foo',
  215. message: 'Hello World',
  216. icon: fs.readFileSync(__dirname + '/coulson.jpg'),
  217. wait: false, // Wait for User Action against Notification
  218. // and other growl options like sticky etc.
  219. sticky: false,
  220. label: undefined,
  221. priority: undefined
  222. });
  223. ```
  224. See more information about using [growly](https://github.com/theabraham/growly/).
  225. ### Usage: `WindowsBalloon`
  226. For earlier versions of Windows, taskbar balloons are used (unless
  227. fallback is activated and Growl is running). The balloons notifier uses a great
  228. project called [**`notifu`**](http://www.paralint.com/projects/notifu/).
  229. ```javascript
  230. const WindowsBalloon = require('node-notifier').WindowsBalloon;
  231. var notifier = new WindowsBalloon({
  232. withFallback: false, // Try Windows Toast and Growl first?
  233. customPath: undefined // Relative/Absolute path if you want to use your fork of notifu
  234. });
  235. notifier.notify(
  236. {
  237. title: undefined,
  238. message: undefined,
  239. sound: false, // true | false.
  240. time: 5000, // How long to show balloon in ms
  241. wait: false, // Wait for User Action against Notification
  242. type: 'info' // The notification type : info | warn | error
  243. },
  244. function (error, response) {
  245. console.log(response);
  246. }
  247. );
  248. ```
  249. See full usage on the [project homepage: **`notifu`**](http://www.paralint.com/projects/notifu/).
  250. ### Usage: `NotifySend`
  251. **Note:** `notify-send` doesn't support the `wait` flag.
  252. ```javascript
  253. const NotifySend = require('node-notifier').NotifySend;
  254. var notifier = new NotifySend();
  255. notifier.notify({
  256. title: 'Foo',
  257. message: 'Hello World',
  258. icon: __dirname + '/coulson.jpg',
  259. wait: false, // Defaults no exipre time set. If true expire time of 5 seconds is used
  260. timeout: 10, // Alias for expire-time, time etc. Time before notify-send expires. Defaults to 10 seconds.
  261. // .. and other notify-send flags:
  262. 'app-name': 'node-notifier',
  263. urgency: undefined,
  264. category: undefined,
  265. hint: undefined
  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. This package is licensed using the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
  324. [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.
  325. [npm-url]: https://npmjs.org/package/node-notifier
  326. [npm-image]: http://img.shields.io/npm/v/node-notifier.svg?style=flat
  327. [npm-downloads]: http://img.shields.io/npm/dm/node-notifier.svg?style=flat
  328. [travis-url]: http://travis-ci.org/mikaelbr/node-notifier
  329. [travis-image]: http://img.shields.io/travis/mikaelbr/node-notifier.svg?style=flat