I am using Electron js and angular to Create a application in that I am sending Notifications through my app using Notification in electron.If my notification are in windows panel or Action Center if I click that is not opening my app instead it is clearing I want it be open my app if I click that notification.
`
import { app, BrowserWindow, session, Event, nativeImage, Tray, ipcMain, Notification, globalShortcut, dialog } from 'electron';
import * as path from 'path';
import * as fs from 'fs';
import log from 'electron-log/main';
let win: BrowserWindow | null = null;
app.setAppUserModelId('Task Tracker Application');
let lastNotification: Notification;
let tray: Tray;
let isQuitting: boolean = false;
const args = process.argv.slice(1),
serve = args.some(val => val === '--serve');
ipcMain.on("Minimize", () => {
log.info('minimize works');
win?.minimize();
});
ipcMain.on("close", () => {
log.info('minimize works');
win?.hide();
showNotification('Task Tracker', 'Please find the app in icon tray.');
});
ipcMain.on("Maximize", () => {
log.info('Maximize Works');
win && (win.isMaximized() ? win.unmaximize() : win.maximize());
});
function createWindow(): BrowserWindow {
log.initialize();
const appData = app.getPath('userData');
log.transports.file.resolvePathFn = () => {
const today = new Date().toISOString().split('T')[0];
return path.join(appData, 'logs', TaskTracker-${today}.log);
};
const logFormat = '{level} | {y}-{m}-{d} {h}:{i}:{s}:{ms} | {text}';
log.transports.console.format = logFormat;
log.info('Log from the main process');
createTray();
win = new BrowserWindow({
x: 0,
y: 0,
width: 1200,
height: 800,
minWidth: 1200,
minHeight: 800,
frame: false,
autoHideMenuBar: true,
icon: path.join(__dirname, 'assets/logo.jpg'),
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: (serve),
contextIsolation: false,
},
titleBarStyle: 'hidden'
});
win.setMenu(null)
win.webContents.openDevTools();
win.webContents.on('did-finish-load', () => {
const css = body::-webkit-scrollbar { display: none; } body { -ms-overflow-style: none; scrollbar-width: none; };
if (win)
win.webContents.insertCSS(css);
});
if (serve) {
const debug = require('electron-debug');
debug();
require('electron-reloader')(module);
win.loadURL('http://localhost:4200');
} else {
let pathIndex = './index.html';
if (fs.existsSync(path.join(__dirname, '../dist/index.html'))) {
pathIndex = '../dist/index.html';
}
const url = new URL(path.join('file:', __dirname, pathIndex));
win.loadURL(url.href);
}
win.on('minimize', function (event: Event) {
event.preventDefault();
if (win)
win.hide();
showNotification('Task Tracker', 'Please find the app in icon tray.');
});
win.on('close', function (event: Event) {
log.info('app quit');
log.info('app-close');
globalShortcut.unregisterAll();
if (!isQuitting) {
event.preventDefault();
if (win)
win.hide();
showNotification('Task Tracker', 'Please find the app in icon tray.');
} else {
win = null;
}
});
win.on('closed', () => {
win = null;
});
return win;
}
function showNotification(title: string, message: string) {
if (lastNotification) {
lastNotification.close()
}
const notification = new Notification({
title: title,
body: message,
icon: path.join(__dirname, 'assets/logo.jpg'),
});
notification.on('click', () => {
if (win === null) {
createWindow();
}
else if (win && !win.isVisible()) {
win.show();
}
else if(win && win.isVisible()) {
win.show();
}
else {
if (win)
win.show();
}
})
notification.show();
lastNotification = notification
}
function createTray() {
const trayIcon = nativeImage.createFromPath(path.join(__dirname, 'assets/logo.jpg'));
tray = new Tray(trayIcon);
tray.setToolTip('Task Tracker');
tray.on('click', () => {
if (win && win.isVisible()) {
win.hide();
} else {
if (win)
win.show();
}
});
}
try {
app.on('ready', () => {
session.defaultSession.allowNTLMCredentialsForDomains('*');
createWindow();
function registerShortcut(key: string, message: string) {
globalShortcut.register(key, () => {
if (win) {
win.webContents.send(message);
if (!win.isVisible()) {
win.show();
}
}
});
}
registerShortcut('ALT+A', 'navigate-to-add-task');
registerShortcut('ALT+D', 'navigate-to-Dashboard');
registerShortcut('ALT+L', 'get-last-updated-tasks');
ipcMain.on('show-notification', (_event, { title, message }) => {
console.log(Received IPC message to show notification. Title: ${title}, Message: ${message});
try {
showNotification(title, message);
console.log('Notification function called successfully.');
} catch (error) {
console.error('Error showing notification:', error);
}
});
});
app.on('before-quit', () => {
globalShortcut.unregisterAll();
log.info('inregister -app');
log.info('app inquit');
isQuitting = true;
});
app.on('window-all-closed', () => {
log.info('inregister -app');
log.info('app quit');
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
} catch (e) {
}`
I am Tried default Notification method but it is opening when notification comes but not opening in windows notification panel or Action Center
`
function showNotification(title: string, message: string) {
if (lastNotification) {
lastNotification.close()
}
const notification = new Notification({
title: title,
body: message,
icon: path.join(__dirname, 'assets/logo.jpg'),
});
notification.on('click', () => {
if (win === null) {
createWindow();
}
else if (win && !win.isVisible()) {
win.show();
}
else if(win && win.isVisible()) {
win.show();
}
else {
if (win)
win.show();
}
})
notification.show();
lastNotification = notification
}`