-
-
Save jflasch/fd16a663434667d2096cd31ec7f2662f to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="renderer.js"></script> | |
<p><button onclick="requestPermission()">Request permission</button></p> | |
<p><button onclick="createMessageNotification()">Generate Notification</button></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Empty */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment