electron notification 通知的使用
electron 的 notification
是走的系统通知通道,Windows上表现为从右边弹出的通知,macOS 为从右上角弹出的通知
一、导入 Notification
const {Notification} = require('electron')
二、新建一个 Notification 实例
在使用通知之前,先要确认系统是否支持 notification,使用 Notification
静态方法 .isSupported()
来获取是否可用
然后新建一个 Notification
实例,传递一个配置对象,具体的配置项目看官方文档:
https://www.electronjs/docs/latest/api/notification#event-show
我们主要用的就是
- title 通知标题
- subtitle 副标题
macOS
- body: 通知内容
最后用当前实例 .show()
显示就好了。
// notification
if (Notification.isSupported()){
console.log('notification is suppoerted')
new Notification({
title: '已成功导出文件',
subtitle: `文件路径:${exportFilePath}`, // macOS
body: `文件路径:${exportFilePath}`
}).show()
}
windows 上的效果就是这样