ส่งการแจ้งเตือนเนทีฟข้ามแพลตฟอร์มโดยใช้ Node.js. ศูนย์การแจ้งเตือนสำหรับ MacOS, notify-osd / libnotify-bin สำหรับ Linux, toasters สำหรับ Windows 8/10 หรือลูกโป่งแถบงานสำหรับรุ่น 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 หรือสูงกว่า หากคุณมีเวอร์ชันก่อนหน้านี้คำรามจะเป็นทางเลือก หากไม่ได้ติดตั้งคำรามข้อผิดพลาดจะถูกส่งกลับในการโทรกลับ
เนื่องจาก node-notifier ล้อมรอบ terminal-notifier คุณสามารถทำสิ่งใดก็ตามที่ terminal-notifier สามารถทำได้เพียงแค่ผ่านคุณสมบัติไปยังวิธี notify
ตัวอย่างเช่น:
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 มีชุดหมด timeout 10 ชุดเพื่อให้แน่ใจว่าแอปพลิเคชันปิดอย่างถูกต้อง เพื่อที่จะลบ 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 , Purr , Sosumi , Submarine , Tink
หาก sound เป็น true ให้ใช้ Bottle
ดูเพิ่มเติม:
การชี้แจงเส้นทางที่กำหนดเอง
customPath ใช้ค่าของเส้นทางสัมพัทธ์หรือสัมบูรณ์ไปยังไบนารีของ terminal-notifier เวอร์ชันส้อม/แบบกำหนดเองของคุณ
ตัวอย่าง: ./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier
คำชี้แจงสปอตไลท์
terminal-notifier.app อยู่ในโฟลเดอร์ mac.noindex เพื่อป้องกันสปอตไลท์จากการจัดทำดัชนีแอพ
WindowsToasterหมายเหตุ: มีข้อ จำกัด บางประการสำหรับรูปภาพในการแจ้งเตือน Windows 8 ดั้งเดิม:
ข้อ จำกัด เหล่านี้เกิดจากระบบการแจ้งเตือนขนมปังปิ้ง เคล็ดลับที่ดีคือการใช้บางอย่างเช่น path.join หรือ path.delimiter เพื่อให้เส้นทางของคุณข้ามแพลตฟอร์ม
จาก mikaelbr/gulp-notify#90 (ความคิดเห็น)
คุณสามารถทำให้มันทำงานได้โดยไปที่ระบบ> การแจ้งเตือนและการกระทำ แอป 'ขนมปังปิ้ง' จำเป็นต้องเปิดใช้งานแบนเนอร์ (คุณสามารถเปิดใช้งานแบนเนอร์ได้โดยคลิกที่แอพ 'ขนมปังปิ้ง' และตั้งค่า 'แสดงแบนเนอร์การแจ้งเตือน' เป็น ON)
การอัปเดตผู้สร้าง Windows 10 Fall (เวอร์ชัน 1709) หมายเหตุ:
Snoretoast ใช้เพื่อรับ Toasts Windows เนทีฟ!
พฤติกรรมเริ่มต้นคือการมีแอปพลิเคชันเครื่องปิ้งขนมปังพื้นฐานเป็น appID สิ่งนี้ใช้งานได้ตามที่คาดไว้ แต่แสดงให้เห็นว่า SnoreToast เป็นข้อความในการแจ้งเตือน
ด้วยการอัปเดตผู้สร้างฤดูใบไม้ร่วงการแจ้งเตือนใน Windows 10 จะใช้งานได้ตามที่คาดไว้หากระบุ appID ที่ถูกต้อง appID ของคุณจะต้องมีค่าเดียวกับที่ลงทะเบียนระหว่างการติดตั้งแอปของคุณ
คุณสามารถค้นหา ID ของแอปของคุณได้โดยค้นหารีจิสทรีสำหรับ appID ที่คุณระบุเมื่อติดตั้งแอปของคุณ ตัวอย่างเช่น: หากคุณใช้เฟรมเวิร์กกระรอก 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
} ) ;ดูข้อมูลเพิ่มเติมเกี่ยวกับการใช้งาน
WindowsBalloon สำหรับ Windows รุ่นก่อนหน้านี้จะใช้ลูกโป่งแถบงาน (เว้นแต่จะเปิดใช้งานทางเลือกและคำรามกำลังทำงานอยู่) Balloons Notifier ใช้โครงการที่ยอดเยี่ยมที่เรียกว่า 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 10 Fall" ในส่วน Windows คำตอบสั้น ๆ : อัปเดต appID ของคุณ
หากคุณไม่เห็นการแจ้งเตือนภายใน WSL2 คุณอาจต้องเปลี่ยนการอนุญาตของไฟล์ผู้ขาย EXE (Snoretoast) ดูปัญหาสำหรับข้อมูลเพิ่มเติม
เมื่อใช้ node-notifier ภายในเซสชัน TMUX อาจทำให้เกิดการแขวนในระบบ สามารถแก้ไขได้โดยทำตามขั้นตอนที่อธิบายไว้ในความคิดเห็นนี้
มีข้อมูลเพิ่มเติมที่นี่ #61 (ความคิดเห็น)
แม้ว่าคุณจะกำหนดไอคอนในวัตถุการกำหนดค่าสำหรับ node-notifier คุณจะเห็นไอคอนเทอร์มินัลขนาดเล็กในการแจ้งเตือน (ดูตัวอย่างที่ด้านบนของเอกสารนี้)
นี่คือวิธีการแจ้งเตือนเกี่ยวกับการทำงานของ MacOS พวกเขามักจะแสดงไอคอนของแอปพลิเคชันหลักที่เริ่มการแจ้งเตือน สำหรับ node-notifier , terminal-notifier เป็นตัวริเริ่มและมีไอคอนเทอร์มินัลที่กำหนดเป็นไอคอน
ในการกำหนดไอคอนที่กำหนดเองของคุณคุณต้องแยก terminal-notifier และสร้างเวอร์ชันที่กำหนดเองด้วยไอคอนของคุณ
ดูปัญหา #71 สำหรับข้อมูลเพิ่มเติม #71
หากบรรจุแอพอิเล็กตรอนของคุณเป็น asar คุณจะพบว่า node-notifier จะไม่โหลด
เนื่องจากวิธีการทำงานของ ASAR คุณไม่สามารถดำเนินการไบนารีจากภายใน asar ได้ เป็นวิธีแก้ปัญหาง่ายๆเมื่อบรรจุแอพลงใน ASAR โปรดตรวจสอบให้แน่ใจว่าคุณ --unpack vendor/ โฟลเดอร์ของ node-notifier ดังนั้นโมดูลยังสามารถเข้าถึงไบนารีการแจ้งเตือนได้
คุณสามารถทำได้ด้วยคำสั่งต่อไปนี้:
asar pack . app.asar --unpack " ./node_modules/node-notifier/vendor/** " หรือถ้าคุณใช้ electron-builder โดยไม่ต้องใช้ ASAR โดยตรงให้ผนวกวัตถุ build เข้ากับ package.json ของคุณ JSON ดังต่อไปนี้:
...
build: {
asarUnpack: [
' ./node_modules/node-notifier/**/* ' ,
]
},
...สำหรับปัญหาที่ใช้กับโมดูล PKG ตรวจสอบปัญหานี้: #220 (ความคิดเห็น)
เมื่อใช้ node-notifier ภายใน webpack คุณต้องเพิ่มตัวอย่างด้านล่างลงใน webpack.config.js ของคุณ
สิ่งนี้จำเป็นเนื่องจาก node-notifier โหลดตัวแจ้งเตือนจากไบนารีดังนั้นจึงต้องใช้เส้นทางไฟล์ที่สัมพันธ์กัน เมื่อ WebPack รวบรวมโมดูลจะระงับ node-notifier ไฟล์ทำให้เกิดข้อผิดพลาดในบางแพลตฟอร์ม
ในการแก้ไขปัญหานี้คุณสามารถกำหนดค่า WebPack เพื่อเก็บไดเรกทอรีไฟล์ที่สัมพันธ์กัน ทำโดยผนวกรหัสต่อไปนี้กับ webpack.config.js ของคุณ:
node: {
__filename : true ,
__dirname : true
} แพ็คเกจนี้ได้รับอนุญาตโดยใช้ใบอนุญาต MIT
Snoretoast และ Notifu มีใบอนุญาตในเวอร์ชัน Vendored ของพวกเขาซึ่งไม่ตรงกับใบอนุญาต MIT, LGPL-3 และ BSD 3 ข้อ จำกัด เราไม่ใช่นักกฎหมาย แต่ได้พยายามอย่างเต็มที่เพื่อให้สอดคล้องกับข้อกำหนดในใบอนุญาตเหล่านั้นในขณะที่ปล่อยแพ็คเกจนี้โดยใช้ใบอนุญาตที่เราเลือก