首页 > 解决方案 > electron :在托盘应用程序中显示基本通知

问题描述

我正在制作一个托盘应用程序,并尝试显示一个基本通知:

我的 main.js 文件和 package.json 如下:

我的 app/main.js 文件

 const path = require('path');
    const {
        app,
        globalShortcut,
        Tray,
        Notification
    } = require('electron');

    app.on('ready', () => {
        tray = new Tray(path.join(__dirname, 'icon-light.png'));

        globalShortcut.register('CommandOrControl+Shift+N', () => {
            console.log("Roger");
            let myNotification = new Notification('Title', {
                body: 'Rabbit'
            })

            myNotification.onclick = () => {
                console.log('Notification clicked')
            }
        });
    })

我的 package.json 文件

{
  "name": "test_notification",
  "version": "1.0.0",
  "description": "",
  "main": "app/main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^5.0.1"
  }
}

当我触发击键 CommandOrControl+Shift+N 时,它会显示 console.log 但不会显示通知。

你知道怎么做吗?

标签: javascriptelectrontray

解决方案


您错过new了以下代码。

let myNotification = new Notification('Title', {
                body: 'Rabbit'
            })

参考:https ://electronjs.org/docs/tutorial/notifications


推荐阅读