Add option for native notifications

This commit is contained in:
KorbsStudio 2023-02-14 14:46:39 -05:00
parent b294184518
commit a579bf48f4
No known key found for this signature in database

View file

@ -1,5 +1,5 @@
// ElectronJS
const {app, BrowserWindow, Menu} = require('electron')
const {app, BrowserWindow, Menu, Notification} = require('electron')
// Dependenies by Korbs Studio
const glasstron = require('glasstron-clarity')
@ -22,9 +22,9 @@ log.transports.console.format = '\x1b[34m[electron]:\x1b[37m'
console.log = log.log
// Use the correct icon depending on the operating system
if /* If macOS */ (process.platform == 'darwin') {global.AppIcon = 'public/images/icons/app/macOS.icns' }
if /* If macOS */ (process.platform == 'darwin') {global.AppIcon = 'public/images/icons/app/macOS.icns' }
else if /* If Windows */ (process.platform == 'win32') {global.AppIcon = 'public/images/icons/app/Windows.png' }
else /* If Linux */ {global.AppIcon = 'public/images/icons/app/Linux.png' }
else /* If Linux */ {global.AppIcon = 'public/images/icons/app/Linux.png' }
function createWindow () {
mainWindow = new glasstron.BrowserWindow({
@ -60,11 +60,16 @@ function createWindow () {
setTimeout(() => {
Pushy.setNotificationListener((data) => {
mainWindow.webContents.executeJavaScript(`
document.querySelector("body > div > div.sidebar > div.sidebar-bottom").insertAdjacentHTML("afterBegin",'<li class="notification"><img class="notification-image" src="${data.image}"><i class="${data.icon}"></i><p class="notification-title">${data.title}</p><p class="notification-message">${data.message}</p><div class="notification-actions"><button id="primary" class="notification-action" onclick="shell.openExternal(${data.action})">Open</button><button id="secondary" class="notification-action">Dismiss</button></div></li>')
`)
if(process.env.NotificationType === 'Native') {
new Notification({ title: data.title, body: data.message }).show()
}
else if(process.env.NotificationType === 'Custom') {
mainWindow.webContents.executeJavaScript(`
document.querySelector("body > div > div.sidebar > div.sidebar-bottom").insertAdjacentHTML("afterBegin",'<li class="notification"><img class="notification-image" src="${data.image}"><i class="${data.icon}"></i><p class="notification-title">${data.title}</p><p class="notification-message">${data.message}</p><div class="notification-actions"><button id="primary" class="notification-action" onclick="shell.openExternal(${data.action})">Open</button><button id="secondary" class="notification-action">Dismiss</button></div></li>')
`)
}
})
}, 10000)
}, 10000) // If this is triggered too soon, the notifications won't work.
if (Pushy.isRegistered()) {Pushy.subscribe('AppName').then(() => {}).catch((error) => {console.error(error)})}