-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
notify.js
47 lines (40 loc) · 1.09 KB
/
notify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const {resolve} = require('path');
const {app, BrowserWindow} = require('electron');
const isDev = require('electron-is-dev');
let win;
// the hack of all hacks
// electron doesn't have a built in notification thing,
// so we launch a window on which we can use the
// HTML5 `Notification` API :'(
let buffer = [];
app.on('ready', () => {
const win_ = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
});
const url = 'file://' + resolve(isDev ? __dirname : app.getAppPath(), 'notify.html');
win_.loadURL(url);
win_.webContents.on('dom-ready', () => {
win = win_;
buffer.forEach(([title, body]) => {
notify(title, body);
});
buffer = null;
});
});
function notify(title, body, details = {}) {
//eslint-disable-next-line no-console
console.log(`[Notification] ${title}: ${body}`);
if (details.error) {
//eslint-disable-next-line no-console
console.error(details.error);
}
if (win) {
win.webContents.send('notification', {title, body});
} else {
buffer.push([title, body]);
}
}
module.exports = notify;