adding actions to notifications

Summary: This diff adds action buttons to the notifications. Notifications with actions can only be sent from the main process. This is why we need to send a message to the main process which then shows the notification. The action callbacks are sent back to the renderer process to handle the action and log the event.

Reviewed By: passy

Differential Revision: D12999886

fbshipit-source-id: b415fded3172582fad11d88cabf0cfc5b3b8d4f9
This commit is contained in:
Daniel Büchele
2018-11-12 01:47:52 -08:00
committed by Facebook Github Bot
parent 4954d018d0
commit 8cb715bb3a
3 changed files with 117 additions and 18 deletions

View File

@@ -8,13 +8,14 @@
const [s, ns] = process.hrtime();
let launchStartTime = s * 1e3 + ns / 1e6;
const {app, BrowserWindow, ipcMain} = require('electron');
const {app, BrowserWindow, ipcMain, Notification} = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
const {exec} = require('child_process');
const compilePlugins = require('./compilePlugins.js');
const os = require('os');
// disable electron security warnings: https://github.com/electron/electron/blob/master/docs/tutorial/security.md#security-native-capabilities-and-your-responsibility
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true;
@@ -168,6 +169,37 @@ ipcMain.on('getLaunchTime', event => {
launchStartTime = null;
}
});
ipcMain.on(
'sendNotification',
(e, {payload, pluginNotification, closeAfter}) => {
// notifications can only be sent when app is ready
if (appReady) {
const n = new Notification(payload);
// Forwarding notification events to renderer process
// https://electronjs.org/docs/api/notification#instance-events
['show', 'click', 'close', 'reply', 'action'].forEach(eventName => {
n.on(eventName, (event, ...args) => {
e.sender.send(
'notificationEvent',
eventName,
pluginNotification,
...args,
);
});
});
n.show();
if (closeAfter) {
setTimeout(() => {
n.close();
}, closeAfter);
}
}
},
);
// Define custom protocol handler. Deep linking works on packaged versions of the application!
app.setAsDefaultProtocolClient('flipper');