Firebase Cloud Messaging 1. Versuch

This commit is contained in:
concierge committed 2026-11-08 12:25:35 +01:00
1 parent f1fb926c2f
commit 4c8b11c4d5
25 files changed
+2111 -31

No files matched your search

+119 -1
View File
@@ -5,11 +5,49 @@
* damit er sowohl die mobilen Views als auch die statischen Assets
* kontrolliert. Der Cache-Name wird pro App-Wurzel gebildet.
*/
const VERSION = 'cmbnet-pwa-v21';
const VERSION = 'cmbnet-pwa-v23';
const BASE = new URL('./', self.location).href;
const SHELL_CACHE = `${VERSION}-shell`;
const TILE_CACHE = `${VERSION}-tiles`;
const MAX_TILES = 400;
const FCM_WAKE_ACK_TIMEOUT_MS = 20000;
// Die Datei wird von Flask erzeugt und enthält nur die öffentliche
// Firebase-Webkonfiguration. Fehlt sie, bleibt FCM in diesem Service Worker
// einfach deaktiviert.
try {
importScripts(new URL('fcm-sw-config.js', self.location).href);
} catch (error) {
self.CMBNET_FCM_CONFIG = {enabled: false};
}
const fcmConfig = self.CMBNET_FCM_CONFIG || {enabled: false};
let fcmMessaging = null;
if (fcmConfig.enabled) {
try {
importScripts(
`https://www.gstatic.com/firebasejs/${fcmConfig.sdkVersion}/firebase-app-compat.js`,
`https://www.gstatic.com/firebasejs/${fcmConfig.sdkVersion}/firebase-messaging-compat.js`
);
if (!firebase.apps.length) {
firebase.initializeApp(fcmConfig.firebase);
}
fcmMessaging = firebase.messaging();
fcmMessaging.onBackgroundMessage(payload => {
const reason = payload
&& payload.data
&& payload.data.reason
? payload.data.reason
: 'wake';
return handleFcmWake(reason);
});
} catch (error) {
// Ohne SDK bleibt die PWA voll funktionsfähig; nur Hintergrund-Wakes
// über FCM stehen dann nicht zur Verfügung.
console.warn('FCM im Service Worker nicht verfügbar:', error);
}
}
const PRECACHE_PATHS = [
'static/css/pico.min.css',
@@ -105,6 +143,78 @@ async function cacheUrls(urls) {
);
}
/* ------------------------------------------------------------------ */
/* FCM-Aufwachsignal */
/* ------------------------------------------------------------------ */
async function handleFcmWake(reason) {
const delivered = await wakeOpenClients(reason);
if (!delivered) {
await showWakeNotification(reason);
}
}
async function wakeOpenClients(reason) {
const clientList = await self.clients.matchAll({
type: 'window',
includeUncontrolled: true
});
const client = clientList.find(item => item.url.startsWith(BASE));
if (!client || typeof client.postMessage !== 'function') {
return false;
}
// Der Service Worker kann keine WebSocket-Verbindung halten. Er
// übergibt den Impuls an die geöffnete PWA und wartet auf deren
// Rückmeldung. So bleibt er lange genug aktiv, dass die Seite die
// Verbindung aufbauen und mindestens ein Positionsupdate senden kann.
const channel = new MessageChannel();
const ack = new Promise(resolve => {
const timer = setTimeout(() => resolve(false), FCM_WAKE_ACK_TIMEOUT_MS);
channel.port1.onmessage = event => {
clearTimeout(timer);
resolve(Boolean(event.data && event.data.ok));
};
channel.port1.start();
});
try {
client.postMessage(
{type: 'cmbnet-wake', reason: reason || 'wake'},
[channel.port2]
);
} catch (error) {
return false;
}
return ack;
}
async function showWakeNotification(reason) {
const body = reason === 'queue'
? 'Neue Nachricht eingegangen. Tippe, um die App zu synchronisieren.'
: 'Die mobile Ansicht wurde zur Synchronisierung aufgefordert.';
const icon = fcmConfig.icon
? new URL(fcmConfig.icon, BASE).href
: new URL('static/img/icon-192.png', BASE).href;
try {
await self.registration.showNotification(fcmConfig.title || '20*C+M+B+27', {
body,
icon,
badge: icon,
tag: fcmConfig.notificationTag || 'cmbnet-wake',
renotify: false,
data: {
type: 'cmbnet-wake',
url: new URL(fcmConfig.wakeUrl || '', BASE).href
}
});
} catch (error) {
// Ohne Benachrichtigungsrecht kann kein sichtbarer Fallback erzeugt
// werden. Der Push selbst bleibt ein reines Signal.
}
}
/* ------------------------------------------------------------------ */
/* Fetch-Strategien */
/* ------------------------------------------------------------------ */
@@ -277,6 +387,14 @@ self.addEventListener('notificationclick', event => {
for (const client of clientList) {
if (client.url.startsWith(BASE) && 'focus' in client) {
// Ein Klick auf ein Aufwachsignal soll die bereits laufende
// PWA zusätzlich synchronisieren, ohne dass FCM Inhalte
// übertragen musste.
if (event.notification.data
&& event.notification.data.type === 'cmbnet-wake'
&& typeof client.postMessage === 'function') {
client.postMessage({type: 'cmbnet-wake', reason: 'notification'});
}
return client.focus();
}
}