Skip to content

Instantly share code, notes, and snippets.

@turbobabr
Last active November 21, 2018 04:02
Show Gist options
  • Save turbobabr/e4021ab39126e3afc8ea1cc01e99b5d0 to your computer and use it in GitHub Desktop.
Save turbobabr/e4021ab39126e3afc8ea1cc01e99b5d0 to your computer and use it in GitHub Desktop.
Electron Fiddle - postNotification issue
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<p></p>
<button id="sender">Post Notification</button>
<script>
require('./renderer.js')
</script>
</body>
</html>
const path = require('path');
const {app, BrowserWindow, ipcMain,systemPreferences,globalShortcut} = require('electron')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', () => {
createWindow();
// Registering global shortcut that could post distributed notification using shortcut when the app is out of focus
globalShortcut.register('Control+Shift+W', () => {
console.log("Posting via shortcut...");
doSendDistributedNotification('shortcut');
});
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
const NOTIFICATION_NAME = 'ELSomeEventFromElectronApp';
// Posting function
function doSendDistributedNotification (trigger) {
systemPreferences.postNotification(NOTIFICATION_NAME,{
trigger
});
}
// Handling 'Post Notification'.
ipcMain.on('postNotification',() => {
console.log("Posting via internal render process...");
doSendDistributedNotification('render process');
});
// Emulating external app that listens to our notification.
systemPreferences.subscribeNotification(NOTIFICATION_NAME,(event,userInfo) => {
console.log('Recieved notification - trigger: '+userInfo.trigger);
});
var {ipcRenderer, remote} = require('electron');
document.getElementById('sender').addEventListener('click',(e) => {
// Posting distributed notification via main process
const ipc = window.ipcRenderer;
ipcRenderer.send('postNotification');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment