v0.12.14 FCM optimization
This commit is contained in:
@@ -75,58 +75,47 @@ function showRosterChirpNotification(data) {
|
||||
});
|
||||
}
|
||||
|
||||
// ── FCM background messages ───────────────────────────────────────────────────
|
||||
// Server sends notification+data messages (mirrors the working fcm-app pattern).
|
||||
// payload.notification carries title/body; payload.data carries url/groupId.
|
||||
// Fallback to payload.data.title/body supports any older data-only messages still
|
||||
// in-flight during a deployment transition.
|
||||
if (messaging) {
|
||||
messaging.onBackgroundMessage((payload) => {
|
||||
console.log('[SW] onBackgroundMessage received:', JSON.stringify({ notification: payload.notification, data: payload.data }));
|
||||
const n = payload.notification || {};
|
||||
const d = payload.data || {};
|
||||
return showRosterChirpNotification({
|
||||
title: n.title || d.title || 'New Message',
|
||||
body: n.body || d.body || '',
|
||||
url: d.url || '/',
|
||||
groupId: d.groupId || '',
|
||||
});
|
||||
});
|
||||
} else {
|
||||
console.warn('[SW] Firebase messaging not initialised — push notifications disabled');
|
||||
}
|
||||
|
||||
// ── Raw push event (fallback for Android) ─────────────────────────────────────
|
||||
// Only runs when Firebase messaging is NOT yet initialised (e.g. push arrives
|
||||
// before firebaseConfigPromise resolves). If Firebase IS ready it already has
|
||||
// its own push listener registered and will call onBackgroundMessage — running
|
||||
// this handler too would show the notification twice.
|
||||
// ── Push handler ──────────────────────────────────────────────────────────────
|
||||
// Unified handler — always uses event.waitUntil so the mobile OS does not
|
||||
// terminate the SW before the notification is shown. Parses event.data
|
||||
// directly (fast, reliable) rather than delegating to the Firebase SDK's
|
||||
// internal push listener, which can be killed before it finishes on Android.
|
||||
self.addEventListener('push', (event) => {
|
||||
console.log('[SW] push event received, hasData:', !!event.data, 'messaging ready:', !!messaging);
|
||||
|
||||
// Firebase compat SDK handles it via onBackgroundMessage — skip to avoid double notification.
|
||||
if (messaging) return;
|
||||
|
||||
if (!event.data) return;
|
||||
console.log('[SW] Push received, hasData:', !!event.data, 'messaging:', !!messaging);
|
||||
|
||||
event.waitUntil((async () => {
|
||||
try {
|
||||
const data = event.data.json();
|
||||
console.log('[SW] Push fallback — data parsed:', JSON.stringify(data));
|
||||
if (data.data || (data.title && data.body)) {
|
||||
await showRosterChirpNotification(data.data || data);
|
||||
let payload = null;
|
||||
|
||||
if (event.data) {
|
||||
try {
|
||||
payload = event.data.json();
|
||||
console.log('[SW] Push data:', JSON.stringify({ notification: payload.notification, data: payload.data }));
|
||||
} catch (e) {
|
||||
console.warn('[SW] Push data not JSON:', e);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[SW] Push fallback — failed to parse push data:', e);
|
||||
const text = event.data.text();
|
||||
if (text) {
|
||||
|
||||
if (payload) {
|
||||
const n = payload.notification || {};
|
||||
const d = payload.data || {};
|
||||
await showRosterChirpNotification({
|
||||
title: n.title || d.title || 'New Message',
|
||||
body: n.body || d.body || '',
|
||||
url: d.url || '/',
|
||||
groupId: d.groupId || '',
|
||||
});
|
||||
} else {
|
||||
// Ghost push — keep SW alive and show a generic notification
|
||||
await self.registration.showNotification('RosterChirp', {
|
||||
body: text.slice(0, 100),
|
||||
body: 'You have a new message.',
|
||||
icon: '/icons/icon-192.png',
|
||||
badge: '/icons/icon-192-maskable.png',
|
||||
tag: 'rosterchirp-fallback',
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[SW] Push handler error:', e);
|
||||
}
|
||||
})());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user