發(fā)布于:2021-02-18 00:00:19
0
348
0
如今,通知可能是上天的恩賜,也可能是我們生存的禍根。你手機(jī)上安裝的每個(gè)應(yīng)用都想要訪問(wèn)通知,桌面應(yīng)用也一樣,現(xiàn)在我們有了一個(gè)Web通知API和一個(gè)Web推送API,以防你的生活中沒(méi)有足夠的通知。日歷上的約會(huì)提醒總是很受歡迎的(否則我就會(huì)忘記每一件事),但Wacky Mini Golf真的需要通知我,我已經(jīng)4天沒(méi)打了嗎?可能不會(huì)。
總之,我在考慮通知,以及如何使用它們來(lái)記住我需要在今天的特定時(shí)間做的事情;例如,提醒自己在不定期的日子去吃午飯、騎自行車或去學(xué)校接兒子。作為一個(gè)JavaScript迷,我決定使用Node.js來(lái)創(chuàng)建Mac通知,我很快找到了答案:node- notification !讓我們一起來(lái)看看!
創(chuàng)建一個(gè)簡(jiǎn)單的通知
node-notifier 在Mac和Windows PC上均可使用。通知的范圍從非常簡(jiǎn)單到高級(jí),所以讓我們首先創(chuàng)建一個(gè)非常簡(jiǎn)單的通知:
const notifier = require('node-notifier');
// String
notifier.notify('Go empty the dishwasher!');
// Object
notifier.notify({
'title': 'David Walsh Blog',
'subtitle': 'Daily Maintenance',
'message': 'Go approve comments in moderation!',
'icon': 'dwb-logo.png',
'contentImage': 'blog.png',
'sound': 'ding.mp3',
'wait': true
});
您可以提供notifier 基本的一樣title,message和icon,然后再進(jìn)一步添加內(nèi)容的圖像,聲音,甚至控制按鈕,顯示通知。
進(jìn)階通知
您可以使用來(lái)創(chuàng)建功能豐富的高級(jí)通知node-notifier,包括回復(fù)功能,控制通知按鈕標(biāo)簽等。以下是更高級(jí)的示例:
const NotificationCenter = require('node-notifier').NotificationCenter;
var notifier = new NotificationCenter({
withFallback: false, // Use Growl Fallback if <= 10.8
customPath: void 0 // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
});
notifier.notify({
'title': void 0,
'subtitle': void 0,
'message': 'Click "reply" to send a message back!',
'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': void 0, // Absolute Path to Attached Image (Content Image)
'open': void 0, // 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: void 0, // String. Label for cancel button
actions: void 0, // String | Array<String>. Action label or list of labels in case of dropdown
dropdownLabel: void 0, // 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(error, response, metadata);
});
這是您的通知可以執(zhí)行的操作類型的一個(gè)快速高峰:
大事記
node-notifier 能夠發(fā)送 click 和close 事件-根據(jù)用戶與通知的交互方式方便地觸發(fā)特定操作:
// Open the DWB website!
notifier.on('click', (obj, options) => {
const spawn = require('child_process').spawn;
const cmd = spawn('open', ['https://davidwalsh.name']);
});
notifier.on('close', (obj, options) => {});
上面的示例使我可以單擊通知以啟動(dòng)我的網(wǎng)站;一個(gè)人也可以使用它來(lái)觸發(fā)他們機(jī)器上的其他例程,當(dāng)然,這僅取決于通知的目的。
您可以在每個(gè)平臺(tái)上獲得有關(guān)Notification對(duì)象和事件的非常詳細(xì)的信息,因此,node-notifier 如果您真的想深入了解,請(qǐng)確保簽出API?;蛘撸绻且粋€(gè)理智的人,也許可以跳過(guò)生活中的更多通知!
作者介紹
熱門博客推薦