node.js 发布系统_使用Node.js的系统通知

node.js 发布系统

Node Notifications

Notifications can be a godsend or the bane of our existence these days.  Every app you install on your phone wants access to notifications, as do desktop apps, and now we have a Web Notifications API along with a Web Push API,  just in case you don't already have enough notifications in your life.  Appointment reminders from Calendar are always welcome (I'd otherwise forget every event) but does Wacky Mini Golf really need to notify me that I haven't played in 4 days?  Probably not.

如今,通知可能是天赐之物或我们生存的祸根。 您在手机上安装的每个应用程序都希望访问通知,桌面应用程序也要访问通知,现在,我们提供了一个Web Notifications API和一个Web Push API ,以防万一您生命中没有足够的通知。 总是欢迎日历中的约会提醒(否则我会忘记每件事),但是Wacky Mini Golf是否真的需要通知我4天没有参加比赛了? 可能不是。

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!  Let's take a look!

无论如何,我一直在思考通知,以及如何使用它们来记住当日特定时间我需要做的事情。 例如,提醒自己去吃午饭,骑自行车或在第二天去接我的儿子上学。 作为一个JavaScript书呆子,我决定研究使用Node.js创建Mac通知,然后我很快找到了答案: node-notifier ! 让我们来看看!

创建一个简单的通知 (Create a Simple Notification)

node-notifier works on both Mac and Windows PCs.  Notifications can range from very simple to advanced so let's first create a very simple notification:

node-notifier可在Mac和Windows PC上使用。 通知的范围从非常简单到高级,因此让我们首先创建一个非常简单的通知:


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
});


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.

您可以提供notifier的基础知识,例如titlemessageicon ,然后进一步添加内容图像,声音,甚至控制通知中显示的按钮。

进阶通知 (Advanced Notifications)

You can create advanced, feature-rich notifications with node-notifier, including the ability to reply, control the notification button labels, and more.  The following is a more advanced example:

您可以使用node-notifier创建高级的,功能丰富node-notifier ,包括回复,控制通知按钮标签等功能。 以下是更高级的示例:


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);
});


Here's a quick peak at the type of actions your notifications can make:

这是您的通知可以执行的操作类型的一个快速高峰:

Notifier

大事记 (Events)

node-notifier is capable of sending click and close events -- handy for triggering specific actions depending on how the user interacts with the notification:

node-notifier能够发送clickclose事件-根据用户与通知的交互方式方便地触发特定操作:


// 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) => {});


The sample above allows me to click on the notification to launch my website; one could also use this to trigger other routines on their machine, of course, it simply depends on what the notification is for.

上面的示例使我可以单击通知以启动我的网站; 人们也可以使用它来触发计算机上的其他例程,当然,这仅取决于通知的用途。

You can get very detailed with your Notification objects and events per platform so be sure to check out the node-notifier API if you really want to dig deep.  Or if you're a sane person, maybe skip out on more notifications in your life!

您可以在每个平台上获得有关Notification对象和事件的非常详细的信息,因此,如果您真的想深入了解,请确保签出node-notifier API。 或者,如果您是个理智的人,也许可以跳过生活中的更多通知!

翻译自: https://davidwalsh.name/system-notifications-node

node.js 发布系统

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值