使用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的特定版本的許可。我們不是律師,而是盡力遵守這些許可證中的條款,同時使用我們選擇的許可證發布此軟件包。