6

我想在电子桌面应用程序中实现通知的粘性行为,直到用户单击通知本身。

我正在使用node-notifier来实现此文档之后的行为,并使用ngx-electron来使用 ElectronService 将 main.js 文件导入角度组件文件中。

这是我的main.js文件:

const notifier = require('node-notifier')
exports.notifier = (msg) =>  {
  notifier.notify({
  title: 'Notify Me',
  message: msg,
  wait: true,
  timeout: 1500000,
  sound: true,
  icon:  './assets/images/logo.png'
});

app.component.ts

import {ElectronService} from 'ngx-electron';
@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  public main_js : any;

  constructor(private _electronService: ElectronService ) { 
    this.main_js  = this._electronService.remote.require("./main.js");
    this.getTasks();
  }

  getTasks() {
    var message = 'New Task Assigned!!!';
    this.main_js.notifier(message);
  }
}

电子应用通知

电子通知

目前,我正在 Windows 平台上检查此通知行为,并且通知保持粘性,直到且除非用户采取任何操作,包括键盘上的任何按键或任何鼠标移动。

我希望通知卡在屏幕上,直到用户单击通知本身的关闭标签,而不是在单击屏幕的任何其他部分时关闭。

4

1 回答 1

1

好吧,我无法在电子中实现通知的粘性行为。但是,我找到了一个很棒的替代方案,即Electron_Tray和 Node-Notifier Balloon_Notifications的组合。

最好的部分是它可以 像魅力一样在WindowsLinux平台上运行,最终提供了跨平台的功能。我还没有在mac上测试过,也许它也可以在那里工作。这是我测试过的代码:

app.component.ts

import {ElectronService} from 'ngx-electron';
  @Component({
    selector: 'app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
  })
  export class AppComponent implements OnInit {
    public main_js : any;

    constructor(private _electronService: ElectronService ) {
      this.main_js  = this._electronService.remote.require("./main.js");
      this.getTasks();
    }

    getTasks() {
      var message = 'New Task Assigned!!!';
      this.main_js.notifier(message);
  }
}

main.js

let tray = null

function createWindow () {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    icon: path.join(__dirname, 'dist/assets/images/logo.png')
  })

  // +++++++  TRAY NOTIFICATIONS  +++++++++ //
  var icon_tray = path.join(__dirname,'dist','assets','images','logo.png');

  tray = new Tray(icon_tray)

  const trayMenuTemplate = [
  {
    label: 'Maximize',
    click: function () {
      //For Maximizing the Window
      if(!win.isVisible()) { win.show() }
    }
  },

  {
    label: 'Minimize',
    click: function () {
      //For Minimizing the Window
      if(win.isVisible()) { win.hide() }
    }
  }]

  tray.setToolTip('I am Notifier!!!')
  let trayMenu = Menu.buildFromTemplate(trayMenuTemplate)
  tray.setContextMenu(trayMenu)
  tray.displayBalloon({
    title: 'Notifier Realm',
    content: 'Greetings!!!',
    icon: icon_tray
  });

  tray.on('click', () => {
    win.isVisible() ? win.hide() : win.show()
  })
}

exports.notifier = (msg) =>  {
// pops out the app window if minimized and show the notification 
  if(win.isVisible()){
    // win.hide()
  } else {
    win.show()
  }
  if(msg != undefined) {
    notifier.notify({
      title: 'Nethues Notify',
      message: msg,
      wait: true,
      icon:  './assets/images/logo.png'
    });
  }
}

现在,每当应用程序窗口被最小化并由不同的用户分配新任务时,该窗口就会在所有应用程序上方弹出(无论您在屏幕上打开什么),并向用户显示新分配的任务通知。

于 2018-08-17T05:03:33.163 回答