Skip to content

Instantly share code, notes, and snippets.

@VerteDinde
Created March 16, 2026 23:40
Show Gist options
  • Select an option

  • Save VerteDinde/20cfd0ba59b69f8fa7f4e71f3645b6e1 to your computer and use it in GitHub Desktop.

Select an option

Save VerteDinde/20cfd0ba59b69f8fa7f4e71f3645b6e1 to your computer and use it in GitHub Desktop.
notification-macos-methods-v1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Notification Static Methods Demo</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, sans-serif; padding: 20px; background: #1e1e1e; color: #ccc; }
h1 { font-size: 18px; color: #fff; }
h2 { font-size: 14px; color: #aaa; margin-top: 24px; }
button { padding: 8px 16px; margin: 4px; border: none; border-radius: 4px; cursor: pointer; font-size: 13px; }
.send { background: #0a84ff; color: #fff; }
.history { background: #30d158; color: #fff; }
.remove { background: #ff9f0a; color: #000; }
.remove-all { background: #ff453a; color: #fff; }
input { padding: 6px 10px; margin: 4px; border: 1px solid #555; border-radius: 4px; background: #2d2d2d; color: #fff; width: 180px; }
#log { margin-top: 20px; padding: 12px; background: #111; border-radius: 6px; font-family: 'SF Mono', Menlo, monospace; font-size: 12px; white-space: pre-wrap; max-height: 300px; overflow-y: auto; }
</style>
</head>
<body>
<h1>Notification Static Methods Demo</h1>
<h2>1. Send Notifications</h2>
<div>
<input id="notif-id" placeholder="id (e.g. msg-1)" />
<input id="notif-group" placeholder="groupId (e.g. chat)" />
</div>
<div>
<input id="notif-title" placeholder="title" value="Hello" />
<input id="notif-body" placeholder="body" value="Test notification" />
</div>
<button class="send" onclick="sendNotification()">Send Notification</button>
<button class="send" onclick="sendBatch()">Send 3 Quick Notifications</button>
<h2>2. Query & Manage</h2>
<button class="history" onclick="getHistory()">getHistory()</button>
<button class="remove" onclick="removeSingle()">remove(id)</button>
<button class="remove-all" onclick="removeAll()">removeAll()</button>
<div id="log">Ready. Send some notifications to get started.</div>
<script>
function log(msg) {
const el = document.getElementById('log');
const ts = new Date().toLocaleTimeString();
el.textContent = `[${ts}] ${msg}\n` + el.textContent;
}
async function sendNotification() {
const id = document.getElementById('notif-id').value || undefined;
const groupId = document.getElementById('notif-group').value || undefined;
const title = document.getElementById('notif-title').value || 'Hello';
const body = document.getElementById('notif-body').value || 'Test';
const result = await window.notificationAPI.send({ id, groupId, title, body });
log(`Sent notification -> id: "${result.id}", groupId: "${result.groupId || ''}"`);
}
async function sendBatch() {
for (let i = 1; i <= 3; i++) {
const result = await window.notificationAPI.send({
id: `batch-${i}`,
groupId: 'batch-group',
title: `Batch #${i}`,
body: `This is batch notification ${i} of 3`
});
log(`Sent batch -> id: "${result.id}"`);
}
log('Batch of 3 notifications sent.');
}
async function getHistory() {
const history = await window.notificationAPI.getHistory();
if (history.length === 0) {
log('getHistory() -> [] (no delivered notifications)');
} else {
log(`getHistory() -> ${history.length} notification(s):\n` +
history.map(n => ` { id: "${n.id}", title: "${n.title}", groupId: "${n.groupId || ''}" }`).join('\n'));
}
}
async function removeSingle() {
const id = document.getElementById('notif-id').value;
if (!id) {
log('Enter an id in the field above to remove a specific notification.');
return;
}
await window.notificationAPI.remove(id);
log(`remove("${id}") called. Check Notification Center.`);
}
async function removeAll() {
await window.notificationAPI.removeAll();
log('removeAll() called. All delivered notifications should be cleared.');
}
</script>
</body>
</html>
const { app, BrowserWindow, Notification, ipcMain } = require('electron');
const path = require('path');
let win;
app.whenReady().then(() => {
win = new BrowserWindow({
width: 700,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
win.loadFile('index.html');
});
// Create and show a notification with a custom id and groupId
ipcMain.handle('send-notification', (_event, { id, groupId, title, body }) => {
const n = new Notification({ id, groupId, title, body });
n.show();
return { id: n.id, groupId: n.groupId };
});
// Get all delivered notifications still in Notification Center
ipcMain.handle('get-history', async () => {
const history = await Notification.getHistory();
return history;
});
// Remove one or more notifications by id
ipcMain.handle('remove-notification', (_event, id) => {
Notification.remove(id);
});
// Remove all delivered notifications
ipcMain.handle('remove-all', () => {
Notification.removeAll();
});
{
"name": "graceful-camp-bless-hwv6i",
"productName": "graceful-camp-bless-hwv6i",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "khammond",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "999.999.999"
}
}
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('notificationAPI', {
send: (opts) => ipcRenderer.invoke('send-notification', opts),
getHistory: () => ipcRenderer.invoke('get-history'),
remove: (id) => ipcRenderer.invoke('remove-notification', id),
removeAll: () => ipcRenderer.invoke('remove-all')
});
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment