【问题标题】:Electron notifications电子通知
【发布时间】:2018-11-19 04:30:07
【问题描述】:

我正在尝试让通知适用于我使用 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");

正如我在一些地方读到的那样,这两个都是使通知工作所必需的。我正在使用 electron forge 构建一个松鼠安装程序,因为也提到了它是获取通知工作所必需的。

我尝试在我的角度组件中使用类似的通知代码,但也没有运气。我已经研究过节点通知器,但无法让它工作,主要是由于缺乏对它应该在 Angular-Electron 应用程序中的位置的了解。

此时我只想让某种形式的桌面通知正常工作,但我找不到任何有关如何在 Angular-Electron 应用程序中执行此操作的资源。

【问题讨论】:

标签: angular notifications electron node-notifier


【解决方案1】:

您还可以使用 Electron service remote 和 node-notifier 模块获取使用 angular 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");
}

ma​​in.js

const notifier = require('node-notifier')

exports.notifier = (msg) =>  {
notifier.notify({
  title: 'Notify Me',
  message: msg,
  wait: true
});

【讨论】:

    【解决方案2】:

    正如上面 Mike 所说,解决方案确实是使用节点通知器。由于它是一个节点模块,我一开始无法直接通过 Angular 工作。经过进一步调查,我发现在 Electron 中,您可以向 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 中有一个侦听器,它侦听此消息并执行所需的节点处理。可能有关于如何做到这一点的教程,但我找不到任何教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多