【问题标题】:How can I open a page by clicking on notification using electron如何通过使用电子单击通知来打开页面
【发布时间】:2018-09-21 10:17:43
【问题描述】:

我正在开发一个通过套接字服务器创建警报/通知的应用程序。因此,我制作了以下组件来实现相同的功能。

  1. 带有节点通知器的套接字服务器

  2. 电子应用

我在这里收到通知,没有任何问题。但是,当我单击通知时,它不会进入以下代码部分。所以我无法处理点击、关闭等事件。

top.notification.on("click", ev =>{ console.log("User Clicked on Notification"); });

我用于 Electron 应用程序的 index.js 文件如下所示。

"use strict";

const appId = "com.myapp.id";
const {
  app,
  nativeImage,
  Tray,
  Menu,
  BrowserWindow,
  Notification
} = require("electron");
app.setAppUserModelId(appId);
let top = {}; // prevent gc to keep windows
app.once("ready", ev => {
  top.win = new BrowserWindow({
    width: 800,
    height: 600,
    center: true,
    minimizable: true,
    show: true,
    webPreferences: {
      nodeIntegration: false,
      webSecurity: true,
      sandbox: true,
    },
  });
  top.win.loadURL("http://localhost:4200");
  top.win.on("close", ev => {
    //console.log(ev);
    ev.sender.hide();
    ev.preventDefault();
  });

  top.notification = new Notification('Title', {
    body: 'Lorem Ipsum Dolor Sit Amet'
  });
  top.notification.on("click", ev => {
    console.log("User Clicked on Notification");
  });
});
app.on("before-quit", ev => {
  top.win.removeAllListeners("close");
  top = null;
});

Socket服务器代码如下

let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let not = require('node-notifier');

io.on('connection', (socket) => {
  var userId = Math.floor(Math.random() * 100)
  // Log whenever a user connects
  console.log(userId, 'user connected');
  var alert = new Object();
  alert.name = 'fire at backyard';
  alert.details = 'fire at backyard hometown  location :lat:1.23424 lang:123434';
  alert.createdBy = 'User';
  alert.createdDate = new Date();
  // Log whenever a client disconnects from our websocket server
  socket.on('disconnect', function() {
    console.log(userId, 'user disconnected');
  });
  // When we receive a 'message' event from our client, print out
  // the contents of that message and then echo it back to our client
  // using `io.emit()`
  socket.on('message', (message) => {
    alert.name = message;
    io.emit('alertGenerated', alert);
    not.notify({
      title: 'My notification',
      message: 'Hello, there!',
      appId: "com.myapp.id"
    });
    console.log("Message send by socket", userId, ":", alert.name);
  });
});
// Initialize our websocket server on port 5000
http.listen(8000, () => {
  console.log('started on port 8000');
});

请帮助我在 Electron 框架中处理我的 Windows 桌面通知的通知事件。

【问题讨论】:

    标签: javascript electron slack node-notifier


    【解决方案1】:

    如果我没有正确理解您的问题,Electron 会使用与浏览器相同的 Notifications API 来显示桌面通知。

    所以要管理点击事件(比如打开一个新窗口),您可以创建一个通知并为点击事件添加一个监听器。就我而言,我使用

    打开新窗口
    newNotification = new Notification('Hi, this is a notification');
    
    newNotification .onclick = function (event) {
        event.preventDefault(); // prevent the browser from focusing the Notification's tab
        window.open("google.com","_blank");
    };
    

    有关window.open() 的电子详细信息,请参阅https://www.electronjs.org/docs/api/window-open

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多