2016-09-21 06:27:11 -08:00
|
|
|
const {resolve} = require('path');
|
|
|
|
|
|
|
|
|
|
const {app, BrowserWindow} = require('electron');
|
2016-07-07 12:48:43 -08:00
|
|
|
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 :'(
|
|
|
|
|
|
2016-07-07 14:17:11 -08:00
|
|
|
let buffer = [];
|
|
|
|
|
|
2016-07-07 12:48:43 -08:00
|
|
|
app.on('ready', () => {
|
|
|
|
|
const win_ = new BrowserWindow({
|
|
|
|
|
show: false
|
|
|
|
|
});
|
2017-09-10 05:35:10 -08:00
|
|
|
const url = 'file://' + resolve(isDev ? __dirname : app.getAppPath(), 'notify.html');
|
2016-07-07 12:48:43 -08:00
|
|
|
win_.loadURL(url);
|
2016-07-07 14:17:11 -08:00
|
|
|
win_.webContents.on('dom-ready', () => {
|
|
|
|
|
win = win_;
|
|
|
|
|
buffer.forEach(([title, body]) => {
|
|
|
|
|
notify(title, body);
|
|
|
|
|
});
|
|
|
|
|
buffer = null;
|
|
|
|
|
});
|
2016-07-07 12:48:43 -08:00
|
|
|
});
|
|
|
|
|
|
2018-05-02 00:10:44 -08:00
|
|
|
function notify(title, body, details = {}) {
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-07-13 12:44:24 -08:00
|
|
|
console.log(`[Notification] ${title}: ${body}`);
|
2018-05-02 00:10:44 -08:00
|
|
|
if (details.error) {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
console.error(details.error);
|
|
|
|
|
}
|
2016-07-07 12:48:43 -08:00
|
|
|
if (win) {
|
2016-09-21 06:27:11 -08:00
|
|
|
win.webContents.send('notification', {title, body});
|
2016-07-07 14:17:11 -08:00
|
|
|
} else {
|
|
|
|
|
buffer.push([title, body]);
|
2016-07-07 12:48:43 -08:00
|
|
|
}
|
2016-07-07 14:17:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = notify;
|