Skip to content

Instantly share code, notes, and snippets.

@jflasch
Last active June 24, 2020 20:00
Show Gist options
  • Save jflasch/fd16a663434667d2096cd31ec7f2662f to your computer and use it in GitHub Desktop.
Save jflasch/fd16a663434667d2096cd31ec7f2662f to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<script src="renderer.js"></script>
<p><button onclick="requestPermission()">Request permission</button></p>
<p><button onclick="createMessageNotification()">Generate Notification</button></p>
const { app, BrowserWindow } = require('electron');
let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({
height: 600,
width: 800,
webPreferences: {
nodeIntegration: false
}
});
mainWindow.loadURL('file://' + __dirname + '/index.html');
});
let notifications = new Map();
let count = 0;
function requestPermission() {
if (!('Notification' in window)) {
alert('Notification API not supported!');
return;
}
console.log('requesting perm');
Notification.requestPermission(function (result) {
console.log('Permission status: ', result);
});
}
function createMessageNotification() {
if (!('Notification' in window)) {
alert('Notification API not supported!');
return;
}
console.log('Creating new notification');
try {
let tag = 'messageNotification';
let oldNotification = notifications.get(tag);
if (oldNotification) {
console.log('Closing old notification: ', oldNotification.body);
oldNotification.close();
notifications.delete(tag);
}
let messageBody = `New Message: ${++count}`;
let notification = new Notification("NOTIFICATION", {
tag,
requireInteraction: true,
body: messageBody
});
notification.onclose = function() {
console.log('Notification Successfully closed: ', notification.body);
}
notification.onerror = function(err) {
console.log('Notification error: ', err);
}
notifications.set(notification.tag, notification);
} catch (err) {
alert('Notification API error: ' + err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment