电子与节点通知程序显示Windows 10通知 [英] Electron with node-notifier display windows 10 notification

查看:97
本文介绍了电子与节点通知程序显示Windows 10通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a simple app that should display notification when button is clicked. The problem is that the notification does not show, but console.logs are showing. Should the notification work on development mode? (meaning just running electron ., and I don't have to build and install the app)

Windows操作系统:


  • 版本:Windows 10 Home

  • 版本:1709

  • 内部版本:16299.98

  • 注意:在 System-> Notification&中启用了Toast(横幅,操作中心)动作

  • Edition: Windows 10 Home
  • Version: 1709
  • Build:16299.98
  • NOTE: Toast is enabled (banner, action center) under System->Notification & Actions

代码:

// 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.com/mikaelbr/node- notifier / issues / 144#issuecomment-319324058

I've got it working now, thanks to all the people here :) https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058

基于 anthonyraymond 的评论,您需要在您的Windows计算机中使用appId来 已安装 。您可以像这样在 package.json 中配置 appId

Based on anthonyraymond's comment, you need to have your app INSTALLED in your windows machine with an appId. You can configure appId in your package.json like this.

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

appId 不需要具有 java / android 格式,我的应用仅具有 elite-notifier appId $ c>。

The appId does not need to have that java/android format, my app just have an appId of elite-notifier.

然后您可以在调用通知 appId

Then you can pass the appId when calling the notify function of notifier.

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 ,因为在已安装的版本和应用程序的开发版本。

After installation, This will work even on development mode (by running electron . command) provided that you'll not change the appId of your app after installation since there will be a mismatch on the installed one and the development version of the app.

这篇关于电子与节点通知程序显示Windows 10通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆