Native Desktop-Notifications gehen in Electron auf zwei Wegen — als Notification-Klasse im Main-Prozess oder als Web-Notification-API im Renderer. Beide enden auf der gleichen System-API, haben aber unterschiedliche Trade-Offs. Dazu Plattform-Unterschiede, die man kennen muss.
Im Main-Prozess
import { Notification } from 'electron';
const notification = new Notification({
title: 'Neue Nachricht',
body: 'Anna hat dir geschrieben.',
icon: path.join(__dirname, 'assets/notify.png'),
silent: false
});
notification.on('click', () => {
mainWindow.show();
});
notification.show();Notification.isSupported() prüft vorher, ob das System Notifications unterstützt — kann auf Linux false sein, falls keine D-Bus-fähige Notification-Daemon läuft.
Im Renderer-Prozess
Wer aus dem Renderer benachrichtigen will, nutzt die Standard-Web-API:
// Berechtigung anfragen (auf manchen Plattformen Pflicht)
const permission = await Notification.requestPermission();
if (permission === 'granted') {
new Notification('Neue Nachricht', {
body: 'Anna hat dir geschrieben.',
icon: '/assets/notify.png'
}).onclick = () => console.log('clicked');
}In Electron ist die Berechtigung default „granted" — keine User-Abfrage. Andere Plattformen (z. B. echtes Web) verlangen die Anfrage explizit.
Plattform-Unterschiede
| Feature | macOS | Windows | Linux |
|---|---|---|---|
| Native System-Integration | Notification Center | Action Center | Distro-abhängig (libnotify) |
| Code-Signing-Pflicht | Nein | Ja (ab Win 10) | Nein |
| App-Identifikation | Bundle-ID | AppUserModelID | .desktop-Datei |
| Reply / Actions | Ja | Ja | Eingeschränkt |
| Bild-Body | Ja | Ja | Distro-abhängig |
| Sound | Optional | Optional | Distro-abhängig |
Windows — AppUserModelID und Code-Signing
Auf Windows 10+ tauchen Notifications ohne Identifikations-Setup nur als „Electron"-Notifications auf, nicht unter dem App-Namen. Lösung:
if (process.platform === 'win32') {
app.setAppUserModelId('com.example.myapp');
}Plus: für „echte" persistente Notifications (im Action Center) muss die App code-signed sein. Unsignierte Apps zeigen Notifications nur transient.
Reply und Actions (macOS, Windows)
const notification = new Notification({
title: 'Neue Nachricht',
body: 'Anna hat dir geschrieben.',
hasReply: true, // macOS
replyPlaceholder: 'Antworten...',
actions: [ // macOS
{ type: 'button', text: 'Markieren als gelesen' }
]
});
notification.on('reply', (_event, reply) => {
console.log('User antwortete:', reply);
});
notification.on('action', (_event, index) => {
console.log('Action geklickt:', index);
});
notification.show();hasReply öffnet ein Inline-Eingabefeld in der Notification (macOS-Pattern). Für Windows-Toast-Actions ist das API ähnlich, aber die Integration tiefer (Toast-XML).
Silent und Urgency
new Notification({
title: 'Backup fertig',
body: 'Alle Dateien gesichert.',
silent: true, // kein Sound
urgency: 'low' // 'low', 'normal' (default), 'critical' — Linux
});urgency wird hauptsächlich von Linux-Notification-Daemons gelesen. macOS und Windows ignorieren es.
Click-Handler und Routing
function notify(message, payload) {
const notification = new Notification({
title: 'My App',
body: message
});
notification.on('click', () => {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.show();
mainWindow.focus();
mainWindow.webContents.send('notification:clicked', payload);
});
notification.show();
}Pattern: Notification-Click bringt das Fenster nach vorne und übergibt den Kontext (welche Notification?) per IPC an den Renderer.
FAQ
Warum erscheinen Windows-Notifications als Electron?
Fehlende AppUserModelID. Mit app.setAppUserModelId('com.example.myapp') setzen — am besten ganz oben in main.js, vor whenReady. Bei electron-builder-Apps: oft schon in den Build-Settings hinterlegt.
Warum verschwinden meine Notifications nach 5 Sekunden auf Windows?
Toast-Notifications sind transient bei unsignierten Apps. Code-Signing ist Voraussetzung dafür, dass sie im Action Center bleiben. Plus: das System merkt sich, ob der User die App stumm geschaltet hat — bei Distribution beachten.
Wann Main-API, wann Web-API im Renderer?
Web-API ist einfacher und der Renderer kennt den Kontext. Main-API hat mehr Optionen (hasReply, actions, silent-Steuerung). Pragmatisch: Renderer-API für simple Notifications, Main-API für Rich-Notifications mit Reply.
Funktionieren Notifications im Hintergrund (App nicht fokussiert)?
Ja — eine Notification kann jederzeit gefeuert werden, auch wenn die App im Hintergrund ist. Der Click-Handler bringt sie typisch nach vorne.
Warum kommt unter Linux keine Notification?
Linux braucht einen aktiven Notification-Daemon (notify-osd, dunst, gnome-shell). Auf manchen Distros (besonders headless) ist keiner installiert. Mit Notification.isSupported() vorher prüfen.
Werden Notifications limitiert (Spam-Schutz)?
macOS und Windows haben Rate-Limits — zu viele Notifications kurz hintereinander werden zusammengefasst oder unterdrückt. Bei Bulk-Events lieber einen Counter zeigen, nicht 50 separate Notifications.