Appearance
通知消息
通知
Mac、Windows、Linux三个操作系统都为应用程序向用户发送通知提供了手段。 在主进程和渲染进程中,显示通知的技术不同的。
对于渲染进程,Electron 方便地允许开发者使用 HTML5 通知 API 发送通知,要在主进程中显示通知,您需要使用 Notification 模块。
渲染进程
渲染进程可以使用 HTML5 通知 API 发送通知。
renderer.js
const bt = document.querySelector('#btn')
bt.addEventListener('click', async () => {
new Notification('斑马兽通知', {
body: '记得每天学习',
})
})
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'" />
<meta
http-equiv="X-Content-Security-Policy"
content="default-src 'self'; script-src 'self'" />
<title>banmashou</title>
</head>
<body>
<button id="btn">发送通知</button>
<div id="files"></div>
<script src="renderer.js"></script>
</body>
</html>
主进程
主进程发送通知需要使用 Notification 模块。
下面是 main.js 主进程发送通知示例
app.whenReady().then(() => {
createWindow()
new Notification({
title: '斑马兽通知',
body: '记得每天学习',
}).show()
})