標籤:

【吐槽翻譯】 Node 系統通知

不完全翻譯自

System Notifications with Node.js

Notifications can be a godsend or the bane of our existence these days.

(實在不明白)

每個安裝在你手機上的app都希望有訪問notifications能力 桌面app也一樣, 現在我們有了Web Notifications API以及一個Web Push API, 他們在你的生活中無處不在。

Appointment reminders from Calendar are always welcome (Id otherwise forget every event) but does Wacky Mini Golf really need to notify me that I havent played in 4 days? Probably not.

(這個就是作者調侃日曆提醒的不準確性,就是用來引出下文的)

Anyways, I was thinking about notifications and how I could use them to remember stuff I needed to do at a certain time during the current day; i.e. remind myself to go eat lunch, go for a bike ride, or go pick my son up from school on the odd day. Being a JavaScript nerd I decided to look into creating Mac notifications using Node.js and I quickly found my answer: node-notifier! Lets take a look!

(notifications 對我有用 如果你想要用Node.js 就用node-notifier這個模塊)

創建一個簡單的Notification

node-notifier 可以用在 Mac 和 Windows 上 可以有多種效果 我們先簡單的使用

const notifier = require(node-notifier);nn// Stringnnotifier.notify(Go empty the dishwasher!);nn// Objectnnotifier.notify({n title: David Walsh Blog,n subtitle: Daily Maintenance,n message: Go approve comments in moderation!,n icon: dwb-logo.png,n contentImage: blog.png,n sound: ding.mp3,n wait: truen});n

You can provide notifier the basics like an title, message, and icon, then go further to add a content image, a sound, and even control the buttons that display in the notification.

我自己實驗了下效果

高級用法

const NotificationCenter = require(node-notifier).NotificationCenter;nnvar notifier = new NotificationCenter({n withFallback: false, // Use Growl Fallback if <= 10.8n customPath: void 0 // Relative/Absolute path to binary if you want to use your own fork of terminal-notifiern});nnnotifier.notify({n title: void 0,n subtitle: void 0,n message: Click "reply" to send a message back!,n sound: false, // Case Sensitive string for location of sound file, or use one of macOS native sounds (see below)n icon: Terminal Icon, // Absolute Path to Triggering Iconn contentImage: void 0, // Absolute Path to Attached Image (Content Image)n open: void 0, // URL to open on Clickn wait: false, // Wait for User Action against Notification or times out. Same as timeout = 5 secondsnn // New in latest version. See `example/macInput.js` for usagen timeout: 5, // Takes precedence over wait if both are defined.n closeLabel: void 0, // String. Label for cancel buttonn actions: void 0, // String | Array<String>. Action label or list of labels in case of dropdownn dropdownLabel: void 0, // String. Label to be used if multiple actionsn reply: false // Boolean. If notification should take input. Value passed as third argument in callback and event emitter.n}, function(error, response, metadata) {n console.log(error, response, metadata);n});n

依舊自己試了一下

這個我覺得可以用於聊天程序

事件

通知有 click close 兩個事件

// Open the DWB website!nnotifier.on(click, (obj, options) => {n const spawn = require(child_process).spawn;n const cmd = spawn(open, [https://davidwalsh.name]);n});nnnotifier.on(close, (obj, options) => {});n

這個可以用於提醒自己搶購

這個是GITHUB地址

mikaelbr/node-notifier

本文Example地址

andypinet/node-examples


推薦閱讀:

前端新人學習路徑(3/26)
Safari 會成為下一個 IE 嗎?
跟上前端的步伐[1] - Flow: JS Static Type Checker
極樂技術周報(第二十期)
grunt 怎樣合併 html ?

TAG:前端开发 |