Kirim Pemberitahuan Asli Platform Cross Menggunakan Node.js. Pusat Pemberitahuan untuk MacOS, notify-osd / libnotify-bin untuk Linux, Toasters untuk Windows 8/10, atau balon bilah tugas untuk versi Windows sebelumnya. Geraman digunakan jika tidak ada persyaratan ini yang terpenuhi. Bekerja dengan baik dengan elektron.
Tunjukkan pemberitahuan asli di macOS, windows, linux:
const notifier = require ( 'node-notifier' ) ;
// String
notifier . notify ( 'Message' ) ;
// Object
notifier . notify ( {
title : 'My notification' ,
message : 'Hello, there!'
} ) ; notify-osd atau libnotify-bin Instaled (Ubuntu harus memilikinya secara default)Lihat Dokumentasi dan Bagan Aliran untuk Pilihan Reporter.
npm install --save node-notifierCLI telah pindah ke proyek terpisah: https://github.com/mikaelbr/node-notifier-cli
Penggunaan standar, dengan fallback lintas platform sebagaimana didefinisikan dalam bagan aliran reporter. Semua opsi di bawah ini akan bekerja dengan cara tertentu di sebagian besar platform.
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
} ) ;Jika Anda ingin kontrol yang sangat halus, Anda dapat menyesuaikan setiap reporter secara individual, memungkinkan Anda untuk menyetel opsi tertentu untuk sistem yang berbeda.
Lihat di bawah untuk dokumentasi di setiap reporter.
Contoh:
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 ( ) ;Atau, jika Anda menggunakan beberapa wartawan (atau Anda malas):
// 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 Penggunaan dan pengaturan parameter yang sama sebagai terminal-notifier .
Pusat pemberitahuan asli membutuhkan MacOS versi 10.8 atau lebih tinggi. Jika Anda memiliki versi sebelumnya, Growl akan menjadi fallback. Jika Growl tidak terpasang, kesalahan akan dikembalikan dalam panggilan balik.
Karena node-notifier membungkus di sekitar terminal-notifier , Anda dapat melakukan apa pun yang dapat dimiliki terminal-notifier , hanya dengan meneruskan properti ke metode notify .
Misalnya:
terminal-notifier mengatakan -message , Anda dapat melakukan {message: 'Foo'}terminal-notifier mengatakan -list ALL , Anda dapat melakukan {list: 'ALL'} .Pemberitahuan adalah fokus utama dari modul ini, jadi daftar dan pengaktifan berfungsi, tetapi mereka tidak didokumentasikan.
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 ) ;
}
) ; CATATAN: Opsi wait adalah singkatan untuk timeout: 5 . Ini hanya menetapkan batas waktu selama 5 detik. Itu tidak membuat pemberitahuan lengket!
Pada versi 6.0 ada set timeout default 10 untuk memastikan bahwa aplikasi ditutup dengan benar. Untuk menghapus timeout dan memiliki pemberitahuan penutupan secara instan (tidak mendukung tindakan), atur timeout ke false . Jika Anda menggunakan action , disarankan untuk menetapkan timeout ke nilai tinggi untuk memastikan pengguna memiliki waktu untuk merespons.
Pengecualian: Jika reply didefinisikan, disarankan untuk menetapkan timeout ke nilai tinggi, atau tidak ada sama sekali.
Untuk pemberitahuan macOS: icon , contentImage , dan semua bentuk reply / actions memerlukan MacOS 10.9.
Suara bisa menjadi salah satunya: Basso , Blow , Bottle , Frog , Funk , Glass , Hero , Morse , Ping , Pop , Purr , Sosumi , Submarine , Tink .
Jika sound true , Bottle digunakan.
Lihat juga:
Klarifikasi jalur khusus
customPath mengambil nilai jalur relatif atau absolut ke biner dari fork/versi kustom dari terminal-notifier .
Contoh: ./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier
Klarifikasi Sorotan
terminal-notifier.app berada di folder mac.noindex untuk mencegah sorotan mengindeks aplikasi.
WindowsToasterCATATAN: Ada beberapa batasan untuk gambar dalam pemberitahuan asli Windows 8:
Keterbatasan ini disebabkan oleh sistem pemberitahuan roti panggang. Tip yang baik adalah menggunakan sesuatu seperti path.join atau path.delimiter untuk menjaga jalur Anda lintas platform.
Dari mikaelbr/gulp-notify#90 (komentar)
Anda dapat membuatnya bekerja dengan pergi ke sistem> pemberitahuan & tindakan. Aplikasi 'Toast' perlu diaktifkan spanduk. (Anda dapat mengaktifkan spanduk dengan mengklik aplikasi 'Toast' dan mengatur 'Show Notification Banners' ke ON)
Pembaruan Windows 10 Fall Creators (Versi 1709) Catatan:
Snoretoast digunakan untuk mendapatkan roti panggang jendela asli!
Perilaku default adalah memiliki applicaton pemanggang yang mendasari sebagai appID . Ini berfungsi seperti yang diharapkan, tetapi menunjukkan SnoreToast sebagai teks dalam pemberitahuan.
Dengan pembaruan Fall Creators, pemberitahuan pada Windows 10 hanya akan berfungsi seperti yang diharapkan jika appID yang valid ditentukan. appID Anda harus memiliki nilai yang persis sama yang terdaftar selama instalasi aplikasi Anda.
Anda dapat menemukan ID aplikasi Anda dengan mencari registri untuk appID yang Anda tentukan saat menginstal aplikasi Anda. Misalnya: Jika Anda menggunakan kerangka kerja tupai, appID Anda akan menjadi sesuatu seperti 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
} ) ;Lihat informasi lebih lanjut tentang penggunaan Growly.
WindowsBalloon Untuk versi Windows sebelumnya, balon taskbar digunakan (kecuali Fallback diaktifkan dan geraman sedang berjalan). Pemberitahuan balon menggunakan proyek hebat yang disebut 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 ) ;
}
) ; Lihat Penggunaan Lengkap di Beranda Proyek: notifu .
NotifySend Catatan: notify-send tidak mendukung bendera 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
} ) ; Lihat Bendera dan Opsi di Halaman Man notify-send(1)
node-notifier dimungkinkan melalui perangkat lunak open source. Terima kasih yang sangat istimewa untuk semua modul yang digunakan node-notifier .
terminal-notifierSnoretoastnotifugrowlyLihat masalah ini oleh Araxeus.
SnoreToast Lihat Catatan pada "Pembaruan Windows 10 Fall Creators" di bagian Windows. Jawaban singkat: Perbarui appID Anda.
Jika Anda tidak melihat pemberitahuan di dalam WSL2, Anda mungkin harus mengubah izin dari file vendor exe (snoretoast). Lihat masalah untuk info lebih lanjut
Saat menggunakan node-notifier dalam sesi TMUX, itu dapat menyebabkan gantung di sistem. Ini dapat diselesaikan dengan mengikuti langkah -langkah yang dijelaskan dalam komentar ini
Bahkan ada info lebih lanjut di sini #61 (komentar).
Bahkan jika Anda mendefinisikan ikon di objek konfigurasi untuk node-notifier , Anda akan melihat ikon terminal kecil dalam pemberitahuan (lihat contoh di bagian atas dokumen ini).
Inilah cara pemberitahuan tentang pekerjaan macOS. Mereka selalu menunjukkan ikon aplikasi induk yang memulai pemberitahuan. Untuk node-notifier , terminal-notifier adalah inisiator, dan memiliki ikon terminal yang didefinisikan sebagai ikonnya.
Untuk menentukan ikon kustom Anda, Anda perlu melakukan fork terminal-notifier dan membangun versi khusus Anda dengan ikon Anda.
Lihat Edisi #71 untuk info lebih lanjut #71.
Jika mengemas aplikasi elektron Anda sebagai asar , Anda akan menemukan node-notifier akan gagal memuat.
Karena cara kerja Asar, Anda tidak dapat mengeksekusi biner dari dalam asar . Sebagai solusi sederhana, saat mengemas aplikasi ke Asar, pastikan Anda --unpack vendor/ folder node-notifier , sehingga modul masih memiliki akses ke binari pemberitahuan.
Anda dapat melakukannya dengan perintah berikut:
asar pack . app.asar --unpack " ./node_modules/node-notifier/vendor/** " Atau jika Anda menggunakan electron-builder tanpa menggunakan Asar secara langsung, tambahkan objek build ke package.json Anda. JSON seperti di bawah ini:
...
build: {
asarUnpack: [
' ./node_modules/node-notifier/**/* ' ,
]
},
...Untuk masalah menggunakan dengan modul PKG. Periksa masalah ini: #220 (komentar)
Saat menggunakan node-notifier di dalam webpack , Anda harus menambahkan cuplikan di bawah ini ke webpack.config.js Anda.
Ini diperlukan karena node-notifier memuat pemberitahuan dari biner, sehingga membutuhkan jalur file relatif. Ketika Webpack mengkompilasi modul, itu menekan direktori file, menyebabkan node-notifier kesalahan pada platform tertentu.
Untuk memperbaikinya, Anda dapat mengonfigurasi webpack untuk menjaga direktori file relatif. Lakukan dengan menambahkan kode berikut ke webpack.config.js Anda:
node: {
__filename : true ,
__dirname : true
} Paket ini dilisensikan menggunakan lisensi MIT.
Snoretoast dan Notifu memiliki lisensi dalam versi vendor mereka yang tidak cocok dengan lisensi MIT, LGPL-3 dan BSD 3-Clause agar spesifik. Kami bukan pengacara, tetapi telah melakukan upaya terbaik kami untuk menyesuaikan diri dengan persyaratan dalam lisensi tersebut saat merilis paket ini menggunakan lisensi yang kami pilih.