0

我尝试在节点服务器上的电子项目中使用通知。所以我使用以下命令在我的 app 文件夹中安装了 node-notifier 模块。

  $ npm install --save node-notifier

之后,我将按钮添加到我的网页以显示通知消息。
当用户单击按钮时,将运行以下 js 方法:

<script type = "text/javascript">
         const notifier = require('node-notifier')
         const path = require('path');
         
         document.getElementById('notify').onclick = (event) => {
            notifier.notify ({
               title: 'My awesome title',
               message: 'Hello from electron, Mr. User!',
               icon: path.join('','images/images.png'),  // Absolute path (doesn't work on balloons)
               sound: true,  // Only Notification Center or Windows Toasters
               wait: true    // Wait with callback, until user action is taken against notification
            }, function (err, response) {
               // Response is response from notification
            });

            notifier.on('click', function (notifierObject, options) {
               console.log("You clicked on the notification")
            });

            notifier.on('timeout', function (notifierObject, options) {
               console.log("Notification timed out!")
            });
         }
      </script>

但是当我点击我的通知按钮时,我收到如下错误:

Uncaught TypeError: _crypto.default.randomFillSync is not a function
        rng @ Project\MyElectronProject\node_modules\uuid\dist\rng.js:19
        v4  @ Project\MyElectronProject\node_modules\uuid\dist\v4.js:17
        getPipeName @ Project\MyElectronProject\node_modules\node-notifier\notifiers\toaster.js:51
        notifyRaw@ Project\MyElectronProject\node_modules\nodenotifier\notifiers\toaster.js:60
        document.getElementById.onclick @notification_index.html:16 

上面的错误是在 rng.js 中抛出的 rng 函数。

function rng() {
  if (poolPtr > rnds8Pool.length - 16) {
    debugger;
    _crypto.default.randomFillSync(rnds8Pool);

    poolPtr = 0;
  }

randomFillSync 方法不存在。我无法理解这个错误,你以前遇到过这个错误吗?感谢您的建议。

我在项目中使用的节点版本:v16.13.1

4

1 回答 1

0

node-notifier 是一个 nodejs 模块,不能在浏览器脚本中使用。

我认为您应该在主进程中调用 node-notifier,然后从渲染器进程与主进程进行通信。

请参考:https ://www.electronjs.org/docs/latest/api/ipc-renderer

于 2022-01-06T10:35:05.730 回答