桌面通知
调用操作系统通知api
# 弹出桌面通知
// frontend/src/views/os/notification/Index.vue
<script>
let content = [
{
type: 'main',
title: '通知标题',
subtitle: '副标题', // macOS系统专有属性
body: '这是通知内容-默认',
silent: true,
},
{
type: 'main',
title: '提示音',
subtitle: '副标题-提示音',
body: '这是通知内容-提示音',
silent: false,
},
{
type: 'main',
title: '点击通知事件',
subtitle: '副标题-点击通知事件',
body: '这是通知内容-点击通知事件',
clickEvent: true
},
{
type: 'main',
title: '关闭通知事件',
subtitle: '副标题-关闭通知事件',
body: '这是通知内容-点击通知事件',
closeEvent: true
},
],
sendNotification (index) {
this.$ipc.send(ipcApiRoute.sendNotification, this.content[index]);
},
</script>
// electron/controller/os.js
/**
* 创建系统通知
*/
sendNotification(args, event) {
const { title, subtitle, body, silent} = args;
if (!Notification.isSupported()) {
return '当前系统不支持通知';
}
let options = {};
if (!_.isEmpty(title)) {
options.title = title;
}
if (!_.isEmpty(subtitle)) {
options.subtitle = subtitle;
}
if (!_.isEmpty(body)) {
options.body = body;
}
if (!_.isEmpty(silent)) {
options.silent = silent;
}
this.service.os.createNotification(options, event);
return true
}
// Make sure to add code blocks to your code group
# 完整代码
上次更新: 2025/04/10, 03:07:49