使用node.js。发送跨平台本机通知。 MACOS的通知中心,linux notify-osd / libnotify-bin ,Windows 8/10的Toasters或用于早期Windows版本的任务栏气球。如果不满足这些要求,则使用咆哮。与电子合作良好。
在MacOS,Windows,Linux上显示本机通知:
const notifier = require ( 'node-notifier' ) ;
// String
notifier . notify ( 'Message' ) ;
// Object
notifier . notify ( {
title : 'My notification' ,
message : 'Hello, there!'
} ) ; notify-osd或libnotify-bin (Ubuntu应该具有此功能)有关记者选择,请参见文档和流程图。
npm install --save node-notifierCLI已移至单独的项目:https://github.com/mikaelbr/node-notifier-cli
标准用法,带有记者流程图中定义的跨平台后备。以下所有选项都将在大多数平台上以某种方式工作。
const notifier = require ( 'node-notifier' ) ;
const path = require ( 'path' ) ;
notifier . notify (
{
title : 'My awesome title' ,
message : 'Hello from node, Mr. User!' ,
icon : path . join ( __dirname , 'coulson.jpg' ) , // Absolute path (doesn't work on balloons)
sound : true , // Only Notification Center or Windows Toasters
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
} ,
function ( err , response , metadata ) {
// Response is response from notification
// Metadata contains activationType, activationAt, deliveredAt
}
) ;
notifier . on ( 'click' , function ( notifierObject , options , event ) {
// Triggers if `wait: true` and user clicks notification
} ) ;
notifier . on ( 'timeout' , function ( notifierObject , options ) {
// Triggers if `wait: true` and notification closes
} ) ;如果您想要超级细粒度的控件,则可以单独自定义每个记者,从而使您可以针对不同系统调整特定选项。
有关每个记者的文档,请参见下文。
例子:
const NotificationCenter = require ( 'node-notifier/notifiers/notificationcenter' ) ;
new NotificationCenter ( options ) . notify ( ) ;
const NotifySend = require ( 'node-notifier/notifiers/notifysend' ) ;
new NotifySend ( options ) . notify ( ) ;
const WindowsToaster = require ( 'node-notifier/notifiers/toaster' ) ;
new WindowsToaster ( options ) . notify ( ) ;
const Growl = require ( 'node-notifier/notifiers/growl' ) ;
new Growl ( options ) . notify ( ) ;
const WindowsBalloon = require ( 'node-notifier/notifiers/balloon' ) ;
new WindowsBalloon ( options ) . notify ( ) ;或者,如果您使用的是几个记者(或者您的懒惰):
// NOTE: Technically, this takes longer to require
const nn = require ( 'node-notifier' ) ;
new nn . NotificationCenter ( options ) . notify ( ) ;
new nn . NotifySend ( options ) . notify ( ) ;
new nn . WindowsToaster ( options ) . notify ( options ) ;
new nn . WindowsBalloon ( options ) . notify ( options ) ;
new nn . Growl ( options ) . notify ( options ) ; NotificationCenter相同的用法和参数设置与terminal-notifier 。
本机通知中心需要MacOS版本10.8或更高版本。如果您有较早的版本,则Growl将是后备。如果未安装咆哮,将在回调中返回错误。
由于node-notifier围绕terminal-notifier包裹,因此您只需将属性传递给notify方法,就可以执行terminal-notifier所能的任何操作。
例如:
terminal-notifier说-message ,则可以执行{message: 'Foo'}terminal-notifier说明-list ALL ,则可以执行{list: 'ALL'} 。通知是该模块的主要重点,因此上市和激活确实有效,但没有记录下来。
const NotificationCenter = require ( 'node-notifier' ) . NotificationCenter ;
var notifier = new NotificationCenter ( {
withFallback : false , // Use Growl Fallback if <= 10.8
customPath : undefined // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
} ) ;
notifier . notify (
{
title : undefined ,
subtitle : undefined ,
message : undefined ,
sound : false , // Case Sensitive string for location of sound file, or use one of macOS' native sounds (see below)
icon : 'Terminal Icon' , // Absolute Path to Triggering Icon
contentImage : undefined , // Absolute Path to Attached Image (Content Image)
open : undefined , // URL to open on Click
wait : false , // Wait for User Action against Notification or times out. Same as timeout = 5 seconds
// New in latest version. See `example/macInput.js` for usage
timeout : 5 , // Takes precedence over wait if both are defined.
closeLabel : undefined , // String. Label for cancel button
actions : undefined , // String | Array<String>. Action label or list of labels in case of dropdown
dropdownLabel : undefined , // String. Label to be used if multiple actions
reply : false // Boolean. If notification should take input. Value passed as third argument in callback and event emitter.
} ,
function ( error , response , metadata ) {
console . log ( response , metadata ) ;
}
) ;注意: wait选项是timeout: 5 。这只是设置了5秒钟的暂停。它不会使通知很粘!
从6.0版开始,有10默认timeout集,以确保应用程序正确关闭。为了删除timeout并立即结束通知(不支持操作),请将timeout设置为false 。如果您使用的是action建议将timeout设置为高价值,以确保用户有时间响应。
异常:如果定义了reply ,建议将timeout设置为高价值,或者根本没有。
对于MACOS通知: icon , contentImage和所有形式的reply / actions都需要MACOS 10.9。
声音可以是其中之一: Basso , Blow , Bottle , Frog , Funk , Glass , Hero , Morse , Ping ,pop, Pop , Purr , Sosumi ,sosumi, Submarine , Tink 。
如果sound简直是true ,则使用Bottle 。
参见:
自定义路径澄清
customPath具有相对或绝对路径的值,即您的fork/自定义版本的terminal-notifier的二进制路径。
示例: ./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier /terminal-notifier.app/contents/macos/terminal-notifier
聚光灯澄清
terminal-notifier.app位于mac.noindex文件夹中,以防止聚光灯索引应用程序。
WindowsToaster注意:本机Windows 8通知中图像有一些局限性:
这些限制是由于敬酒通知系统。一个好的提示是使用诸如path.join或path.delimiter之类的东西保持路径跨平台。
来自Mikaelbr/Gulp-Notify#90(评论)
您可以通过进入系统>通知和操作来使其起作用。 “ Toast”应用需要启用横幅。 (您可以通过单击“ Toast”应用程序并将“显示通知横幅”设置为ON来激活横幅)
Windows 10 Fall Creators Update(版本1709)注意:
SnoreToast用于获取本机Windows Toasts!
默认行为是将基础烤面包机应用作为appID 。这是按预期工作的,但在通知中SnoreToast为文本。
随着Fall Creators的更新,Windows 10上的通知只有指定了有效的appID ,才能按预期工作。您的appID必须与安装应用程序期间注册的值完全相同。
您可以通过搜索注册表来找到应用appID来找到应用程序的ID。例如:如果您使用松鼠框架,则您的appID将像com.squirrel.your.app类的东西。
const WindowsToaster = require ( 'node-notifier' ) . WindowsToaster ;
var notifier = new WindowsToaster ( {
withFallback : false , // Fallback to Growl or Balloons?
customPath : undefined // Relative/Absolute path if you want to use your fork of SnoreToast.exe
} ) ;
notifier . notify (
{
title : undefined , // String. Required
message : undefined , // String. Required if remove is not defined
icon : undefined , // String. Absolute path to Icon
sound : false , // Bool | String (as defined by http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx)
id : undefined , // Number. ID to use for closing notification.
appID : undefined , // String. App.ID and app Name. Defaults to no value, causing SnoreToast text to be visible.
remove : undefined , // Number. Refer to previously created notification to close.
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.
} ,
function ( error , response ) {
console . log ( response ) ;
}
) ;Growl const Growl = require ( 'node-notifier' ) . Growl ;
var notifier = new Growl ( {
name : 'Growl Name Used' , // Defaults as 'Node'
host : 'localhost' ,
port : 23053
} ) ;
notifier . notify ( {
title : 'Foo' ,
message : 'Hello World' ,
icon : fs . readFileSync ( __dirname + '/coulson.jpg' ) ,
wait : false , // Wait for User Action against Notification
// and other growl options like sticky etc.
sticky : false ,
label : undefined ,
priority : undefined
} ) ;查看有关使用Growly的更多信息。
WindowsBalloon对于早期版本的Windows,使用了任务栏气球(除非激活后返回并运行咆哮)。气球通知器使用一个名为notifu的伟大项目。
const WindowsBalloon = require ( 'node-notifier' ) . WindowsBalloon ;
var notifier = new WindowsBalloon ( {
withFallback : false , // Try Windows Toast and Growl first?
customPath : undefined // Relative/Absolute path if you want to use your fork of notifu
} ) ;
notifier . notify (
{
title : undefined ,
message : undefined ,
sound : false , // true | false.
time : 5000 , // How long to show balloon in ms
wait : false , // Wait for User Action against Notification
type : 'info' // The notification type : info | warn | error
} ,
function ( error , response ) {
console . log ( response ) ;
}
) ;请参阅项目主页上的全部用法: notifu 。
NotifySend注意: notify-send不支持wait标志。
const NotifySend = require ( 'node-notifier' ) . NotifySend ;
var notifier = new NotifySend ( ) ;
notifier . notify ( {
title : 'Foo' ,
message : 'Hello World' ,
icon : __dirname + '/coulson.jpg' ,
wait : false , // Defaults no expire time set. If true expire time of 5 seconds is used
timeout : 10 , // Alias for expire-time, time etc. Time before notify-send expires. Defaults to 10 seconds.
// .. and other notify-send flags:
'app-name' : 'node-notifier' ,
urgency : undefined ,
category : undefined ,
hint : undefined
} ) ;请参阅“人”页面上的标志和选项notify-send(1)
通过开源软件使node-notifier成为可能。非常特别感谢所有模块node-notifier用途。
terminal-notifierSnoretoastnotifugrowly请参阅Araxeus的这个问题。
SnoreToast文本请参阅Windows部分中的“ Windows 10 Fall Creators Update”上的注释。简短答案:更新您的appID 。
如果您在WSL2中没有看到通知,则可能必须更改EXE供应商文件(SNORETOAST)的许可。有关更多信息,请参见问题
在TMUX会话中使用node-notifier时,可能会导致系统中的挂起。可以通过遵循此评论中描述的步骤来解决
这里有更多信息#61(评论)。
即使您在node-notifier的配置对象中定义了图标,您也会在通知中看到一个小终端图标(请参阅本文档顶部的示例)。
这是MacOS工作的通知方式。他们总是显示启动通知的父申请的图标。对于node-notifier , terminal-notifier是启动器,并且其终端图标定义为其图标。
要定义您的自定义图标,您需要使用图标构造terminal-notifier并构建自定义版本。
有关更多信息71,请参见第71期。
如果将电子应用程序包装为asar ,您会发现node-notifier将无法加载。
由于ASAR的工作方式,您无法从asar内部执行二进制文件。作为一个简单的解决方案,将应用程序包装到ASAR中时,请确保您 - 请您--unpack node-notifier的vendor/文件夹,因此该模块仍然可以访问通知二进制文件。
您可以使用以下命令这样做:
asar pack . app.asar --unpack " ./node_modules/node-notifier/vendor/** "或者,如果您在不直接使用ASAR的情况下使用electron-builder ,请将build对象附加到您的package.json如下:
...
build: {
asarUnpack: [
' ./node_modules/node-notifier/**/* ' ,
]
},
...对于使用PKG模块的问题。检查此问题:#220(评论)
在webpack内部使用node-notifier时,必须将下面的摘要添加到webpack.config.js 。
这是必要的,因为node-notifier从二进制中加载了通知器,因此它需要相对文件路径。当WebPack编译模块时,它会抑制文件目录,从而导致node-notifier在某些平台上发生错误。
要解决此问题,您可以配置WebPack以保留相对文件目录。通过将以下代码附加到您的webpack.config.js :
node: {
__filename : true ,
__dirname : true
} 此软件包使用MIT许可证获得许可。
SnoreToast和Notifu在其供应商版本中具有与MIT许可,LGPL-3和BSD 3-CARES的特定版本的许可。我们不是律师,而是尽力遵守这些许可证中的条款,同时使用我们选择的许可证发布此软件包。