Electron 带有节点通知程序显示 Windows 10 通知

2023-12-14

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

Windows操作系统:

  • 版本:Windows 10 家庭版
  • 版本:1709
  • 内部版本:16299.98
  • 注意:Toast 已在下面启用(横幅、操作中心)System->Notification & Actions

Code:

// 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

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

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

The appId不需要那个java/android格式,我的应用程序只有一个appId of elite-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 . command)前提是您不会更改appId安装后您的应用程序的版本,因为安装的应用程序和应用程序的开发版本会不匹配。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Electron 带有节点通知程序显示 Windows 10 通知 的相关文章

  • 从节点服务器访问 Google Calendar API

    由于某种原因 我很难访问 Google 日历 我希望能够在 Node js 服务器的日历中添加和删除事件 我从文件中发现了非常矛盾的信息 我跟着 https developers google com identity protocols
  • @mailchimp/mailchimp_marketing/types.d.ts'不是nodeJs中的模块

    我在 NodeJS 应用程序中导入了 import mailchimp mailchim marketing import mailchimp from mailchimp mailchimp marketing 但是 它给出以下错误 ty
  • 从 websocket 发送数据到 socket.io

    我使用 websocket 接口连接到 websocket 服务器 如果我想将通过 websocket 接口从 websocket 服务器接收到的数据发送到通过 http 服务器连接到我的客户端 我应该使用 socket io 吗 所以最后
  • Express中间件修改请求

    我目前有一个正在运行的服务器 前端使用nodejs mongo express 和 W2UI W2ui 请求来自包含所有参数的记录数组 记录 名称 foo 我想编写一个中间件 在请求到达路由之前对其进行编辑和更改 您可以创建自己的中间件来处
  • Browserify - 如何在浏览器中调用通过 browserify 生成的文件中捆绑的函数

    我是 Nodejs 和 browserify 的新手 我从这个开始link http browserify org 我有文件 main js 其中包含此代码 var unique require uniq var data 1 2 2 3
  • API 使用令牌向 odoo 进行身份验证

    我想使用令牌从 Express 应用程序向 Odoo 进行身份验证 我在用odoo xmlrpc https www npmjs com package odoo xmlrpc连接 Odoo 的节点模块 我的快递应用程序 Odoo 要求 A
  • Nodejs 调试生产中的错误

    我有一个在生产环境中运行的 Nodejs 脚本 我不太可能 千分之一 遇到这样的错误 TypeError value is out of bounds at checkInt buffer js 1009 11 at Buffer writ
  • 如何获取nodejs程序中的nodejs版本?

    In a Node js 的调试器 https github com rocky trepanjs 有一个命令显示V8版本和调试器包版本 如何获取nodejs版本 我想我基本上可以运行命令node version or nodejs ver
  • 如何为我的整个 Node.js 应用程序使用相同的 MySQL 连接?

    我有一个app js 我从那里运行我的整个应用程序 在 app js 内部 我require许多文件中都有代码 对于每个文件 我都这样做 var mysql require mysql var mclient mysql createCon
  • Bot Framework Node.js 发送给特定用户的临时消息

    我已经盯着这个问题好几个小时了 找不到解决方案 即使根据所有建议 它应该很容易 https learn microsoft com en us bot framework nodejs bot builder nodejs proactiv
  • 使用 React.js + Express.js 发送电子邮件

    我在 ES6 中使用 React js 构建了一个 Web 应用程序 我目前想要创建一个基本的 联系我们 页面并想要发送电子邮件 我是 React 新手 刚刚发现我实际上无法使用 React 本身发送电子邮件 我正在遵循教程nodemail
  • 由于 Chrome 修订,Firebase puppeteer PDF 功能超时

    我有一个 Firebase 函数来创建 PDF 文件 最近 由于 Chrome 修订版 而超时 我既不明白错误消息 也不明白出了什么问题 当我在 MacOS 下将其本地部署时 该功能有效 TimeoutError Timed out aft
  • libxmljs 的替代品 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 目标 使用 Node js 访问网页 使用 xpath 语法操作 DOM 并打印新的 DOM libxm
  • 如何在Windows服务器上将node.js文件作为后台进程运行?

    我正在创建一个 node js 项目并将其上传到我的 Windows 服务器 以为移动应用程序提供 API 服务 当我打开命令提示符并键入 node app js 它运行正常 但是当我关闭命令提示符时 我的 Node js 服务器停止运行
  • 节点无法抓取某些页面

    我不知道这是否与冷融合页面有关 但我无法刮掉这些 cfm pages 在目录中的命令行中request run node gt var request require request node gt var url http linguis
  • editMessageReplyMarkup 方法删除内联键盘

    我正在使用 node js 制作一个电报机器人node telegram bot api图书馆 我回答callback query并想更换我的内联键盘 下面的代码显示了我如何尝试使用此方法 但是当我在电报中点击键盘时 它就消失了 bot o
  • Cosmos DB Mongo API 如何管理“请求率很大”情况

    我有以下代码 async function bulkInsert db collectionName documents try const cosmosResults await db collection collectionName
  • nodemon 安装错误“没有可用于超时的有效版本”

    尝试在全新的节点项目中安装 nodemon 时出现此错误 我创建了一个名为 my project 的空白文件夹 然后 在其中 我执行了创建一个 package json 文件 npm init f 然后当尝试运行时 npm install
  • NodeJS hmac 摘要问题与重音

    我正在对以下代码与 Ruby PHP 和 NodeJS 进行并排比较 使用 NodeJS 得到错误的响应crypto module PHP hash hmac sha256 text Ruby OpenSSL HMAC hexdigest
  • 如何使用Create React App安装React

    嗨 我对反应真的很陌生 我不知道如何实际安装它 也不知道我需要做什么才能在其中编写代码 我下载了node js并且安装了v12 18 3以及NPM 6 14 6 但是每次我尝试在许多网站上提到的create react app安装方法中输入

随机推荐