This commit is contained in:
2026-03-23 08:14:02 -04:00
parent 2d164958d8
commit 14c80f436a
4 changed files with 92 additions and 33 deletions

View File

@@ -112,7 +112,10 @@ router.post('/unsubscribe', authMiddleware, async (req, res) => {
} catch (e) { res.status(500).json({ error: e.message }); }
});
// Send a test push to the requesting user's own device — for diagnosing FCM setup
// Send a test push to the requesting user's own device — for diagnosing FCM setup.
// mode=data (default): data-only message handled by the service worker onBackgroundMessage.
// mode=browser: webpush.notification message handled by Chrome directly (bypasses SW).
// Use mode=browser to check if FCM delivery itself works when the SW is not involved.
router.post('/test', authMiddleware, async (req, res) => {
try {
const subs = await query(req.schema,
@@ -130,25 +133,42 @@ router.post('/test', authMiddleware, async (req, res) => {
return res.status(503).json({ error: 'Firebase Admin not initialised on server — check FIREBASE_SERVICE_ACCOUNT in .env' });
}
const mode = req.query.mode === 'browser' ? 'browser' : 'data';
const results = [];
for (const sub of subs) {
try {
await messaging.send({
const message = {
token: sub.fcm_token,
data: {
title: 'RosterChirp Test',
body: 'Push notifications are working! 🎉',
url: '/',
groupId: '',
},
android: { priority: 'high' },
webpush: { headers: { Urgency: 'high' } },
});
results.push({ device: sub.device, status: 'sent' });
console.log(`[Push] Test notification sent to user ${req.user.id} device=${sub.device}`);
};
if (mode === 'browser') {
// Chrome displays the notification directly — onBackgroundMessage does NOT fire.
// Use this to verify FCM delivery works independently of the service worker.
message.webpush.notification = {
title: 'RosterChirp Test (browser)',
body: 'FCM delivery confirmed — Chrome handled this directly.',
icon: '/icons/icon-192.png',
};
message.webpush.fcm_options = { link: '/' };
} else {
// data-only — service worker onBackgroundMessage must show the notification.
message.data = {
title: 'RosterChirp Test',
body: 'Push notifications are working!',
url: '/',
groupId: '',
};
}
await messaging.send(message);
results.push({ device: sub.device, mode, status: 'sent' });
console.log(`[Push] Test (${mode}) sent to user ${req.user.id} device=${sub.device}`);
} catch (err) {
results.push({ device: sub.device, status: 'failed', error: err.message, code: err.code });
console.error(`[Push] Test notification failed for user ${req.user.id} device=${sub.device}:`, err.message);
results.push({ device: sub.device, mode, status: 'failed', error: err.message, code: err.code });
console.error(`[Push] Test (${mode}) failed for user ${req.user.id} device=${sub.device}:`, err.message);
}
}
res.json({ results });