首页 文章

电子通知

提问于
浏览
2

我正在尝试使用Angular 5和Electron制作的Electron应用程序的通知 . 到目前为止,我在index.html文件中有以下代码:

<script type="text/javascript">

  function doNotify() {
      new Notification( "Basic Notification", "Short message part");
  }

   window.onload = doNotify;
  </script>

在我的package.json中,我有appId设置如下:

"build": {
    "appId":"com.myapp.id"
  },

最后我在main.js中有这个:

app.setAppUserModelId("com.myapp.id");

正如我在某些地方读到的那样,这些都是通知工作所必需的 . 我正在使用电子伪造来构建一个松鼠安装程序,因为这也被要求提供通知工作 .

我尝试在我的角度组件中使用类似的通知代码,但也没有运气 . 我已经研究过node-notifier但是我无法让它工作,主要是因为他们不了解它应该在Angular-Electron应用程序中的位置 .

在这一点上,我想要的是获得某种形式的桌面通知工作,但我找不到任何关于如何在Angular-Electron应用程序中执行此操作的资源 .

2 回答

  • 1

    您还可以使用 Electron service remotenode-notifier模块获取使用角度5的电子通知:

    app.component.ts

    import { ElectronService } from 'ngx-electron';
    
    constructor(private databaseService: DatabaseService, private router: Router, private 
     _electronService: ElectronService){
    }
    
    ngOnInit(): void {
      let main_js  = this._electronService.remote.require("./main.js");
      this.main_js.notifier("Message");
    }
    

    main.js

    const notifier = require('node-notifier')
    
    exports.notifier = (msg) =>  {
    notifier.notify({
      title: 'Notify Me',
      message: msg,
      wait: true
    });
    
  • 2

    正如迈克上面所说,解决方案确实与节点通知器一起使用 . 由于它是一个节点模块,我一开始无法直接通过角度工作 . 经过进一步调查后,我发现在Electron中你可以向ipcRenderer发送消息,然后ipcRenderer可以触发节点代码/模块 . 以下是我用来实现此功能的代码:

    在我想要通知的角度文件中,我添加了:

    import { ElectronService } from 'ngx-electron';
    //
    //Other regular angular code here
    //
    constructor(private databaseService: DatabaseService, private router: Router, private 
         _electronService: ElectronService){
    }
    
    ngOnInit(): void {
        this._electronService.ipcRenderer.send('request-mainprocess-action', "Message");
    }
    

    然后在我的main.js中添加了以下内容:

    const {ipcMain} = require('electron');
    var notifier = require('node-notifier');
    //
    //Other regular electron main code
    //
    // Attach listener in the main process with the given ID
    ipcMain.on('request-mainprocess-action', (event, arg) => {
        notifier
        .notify({title: 'Title', message: 'Message', icon:`${__dirname}\\assets\\image.png`,  wait: true }, function(err, data) {
          console.log(err, data);
        })
    });
    

    上面的代码中发生的是使用标记'request-mainprocess-action'将消息发送到ipcRenderer . 然后我在main.js中有一个监听器,它监听此消息并执行所需的节点处理 . 可能有关于如何做到这一点的教程但我无法找到任何 .

相关问题