最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

具有节点通知器的电子显示窗口10通知

IT培训 admin 6浏览 0评论

具有节点通知器的电子显示窗口10通知

我正在尝试创建一个简单的应用程序,它应该在单击按钮时显示通知。问题是通知没有显示,但是console.logs正在显示。通知应该在开发模式下工作吗? (意思是只运行electron .,我不必构建和安装应用程序)

Windows操作系统:

  • 版本:Windows 10 Home
  • 版本:1709
  • 构建:16299.98
  • 注意:在System->Notification & Actions下启用Toast(横幅,操作中心)

码:

// main.js
const { app, BrowserWindow, ipcMain, Notification } = require("electron");

const path = require("path");
const url = require("url");

let win;

function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({ width: 800, height: 600 });

  // and load the index.html of the app.
  win.loadURL(
    url.format({
      pathname: path.join(__dirname, "index.html"),
      protocol: "file:",
      slashes: true
    })
  );

  // Open the DevTools.
  // win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on("closed", () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null;
  });
}

const appId = "elite-notifier";

app.setAppUserModelId(appId);
app.on("ready", createWindow);

console.log("notifying");
ipcMain.on("notify", () => {
  console.log("notified");
  const WindowsToaster = require("node-notifier").WindowsToaster;
  const notifier = new WindowsToaster({
    withFallback: false
  });
  notifier.notify(
    {
      title: "My awesome title",
      message: "Hello from node, Mr. User!",
      sound: true, // Only Notification Center or Windows Toasters
      wait: false // Wait with callback, until user action is taken against notification
    },
    function(err, response) {
      // Response is response from notification
      console.log("responded...");
    }
  );
});


// index.html
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Hello World!</title>
</head>

<body>
  <h1>Notifier!</h1>
  <button type="button" id="notify">Click here to trigger a notification!</button>
      <script type="text/javascript">
         const { ipcRenderer } = require('electron');

         const button = document.getElementById('notify');
         console.log('BUTTON: ', button)
         button.addEventListener('click', function(event) {
            console.log('clicked...');
            ipcRenderer.send('notify')
         });
      </script>
</body>

</html>
回答如下:

我现在已经开始工作了,感谢这里的所有人:) https://github/mikaelbr/node-notifier/issues/144#issuecomment-319324058

根据anthonyraymond的评论,你需要在你的Windows机器上使用appId你的应用程序INSTALLED。您可以像这样在appId中配置package.json

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "test",
  "main": "main.js",
  "build": {
    "appId": "com.myapp.id"
  }
}

appId不需要有java/android格式,我的应用程序只有appIdelite-notifier

然后你可以在调用通知器的appId函数时传递notify

notifier.notify(
    {
      appName: "com.myapp.id", <-- yes, the key here is appName :)
      title: "Hello",
      message: "Hello world!",
      wait: true
    },
    function(err, response) {
      // Response is response from notification
      console.log("responded...");
    }
  );

安装后,即使在开发模式下运行(通过运行electron .命令)也是如此,前提是您在安装后不会更改应用程序的appId,因为安装的应用程序与应用程序的开发版本不匹配。

具有节点通知器的电子显示窗口10通知

我正在尝试创建一个简单的应用程序,它应该在单击按钮时显示通知。问题是通知没有显示,但是console.logs正在显示。通知应该在开发模式下工作吗? (意思是只运行electron .,我不必构建和安装应用程序)

Windows操作系统:

  • 版本:Windows 10 Home
  • 版本:1709
  • 构建:16299.98
  • 注意:在System->Notification & Actions下启用Toast(横幅,操作中心)

码:

// main.js
const { app, BrowserWindow, ipcMain, Notification } = require("electron");

const path = require("path");
const url = require("url");

let win;

function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({ width: 800, height: 600 });

  // and load the index.html of the app.
  win.loadURL(
    url.format({
      pathname: path.join(__dirname, "index.html"),
      protocol: "file:",
      slashes: true
    })
  );

  // Open the DevTools.
  // win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on("closed", () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null;
  });
}

const appId = "elite-notifier";

app.setAppUserModelId(appId);
app.on("ready", createWindow);

console.log("notifying");
ipcMain.on("notify", () => {
  console.log("notified");
  const WindowsToaster = require("node-notifier").WindowsToaster;
  const notifier = new WindowsToaster({
    withFallback: false
  });
  notifier.notify(
    {
      title: "My awesome title",
      message: "Hello from node, Mr. User!",
      sound: true, // Only Notification Center or Windows Toasters
      wait: false // Wait with callback, until user action is taken against notification
    },
    function(err, response) {
      // Response is response from notification
      console.log("responded...");
    }
  );
});


// index.html
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Hello World!</title>
</head>

<body>
  <h1>Notifier!</h1>
  <button type="button" id="notify">Click here to trigger a notification!</button>
      <script type="text/javascript">
         const { ipcRenderer } = require('electron');

         const button = document.getElementById('notify');
         console.log('BUTTON: ', button)
         button.addEventListener('click', function(event) {
            console.log('clicked...');
            ipcRenderer.send('notify')
         });
      </script>
</body>

</html>
回答如下:

我现在已经开始工作了,感谢这里的所有人:) https://github/mikaelbr/node-notifier/issues/144#issuecomment-319324058

根据anthonyraymond的评论,你需要在你的Windows机器上使用appId你的应用程序INSTALLED。您可以像这样在appId中配置package.json

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "test",
  "main": "main.js",
  "build": {
    "appId": "com.myapp.id"
  }
}

appId不需要有java/android格式,我的应用程序只有appIdelite-notifier

然后你可以在调用通知器的appId函数时传递notify

notifier.notify(
    {
      appName: "com.myapp.id", <-- yes, the key here is appName :)
      title: "Hello",
      message: "Hello world!",
      wait: true
    },
    function(err, response) {
      // Response is response from notification
      console.log("responded...");
    }
  );

安装后,即使在开发模式下运行(通过运行electron .命令)也是如此,前提是您在安装后不会更改应用程序的appId,因为安装的应用程序与应用程序的开发版本不匹配。

发布评论

评论列表 (0)

  1. 暂无评论