From f943bd9b7c9b619c363eeffa94468c8d455a9ad2 Mon Sep 17 00:00:00 2001 From: Wizznu Date: Fri, 10 Apr 2026 18:17:56 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix:=20Handle=20FCM=20messaging?= =?UTF-8?q?=20gracefully=20-=20fallback=20when=20not=20available?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- worker.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/worker.js b/worker.js index 8a0c18c..d37c03a 100644 --- a/worker.js +++ b/worker.js @@ -125,7 +125,19 @@ const db = admin.database(); async function sendAutomationNotification({ title, body, type, data = {} }) { try { - await admin.messaging().send({ + // Check if messaging is available + if (!admin.messaging) { + console.log(`📝 [INFO] Cloud Messaging not available. App will use local notifications via Firebase listener.`); + return; + } + + const messaging = admin.messaging(); + if (!messaging) { + console.log(`📝 [INFO] Cloud Messaging SDK not initialized. App will use local notifications via Firebase listener.`); + return; + } + + const result = await messaging.send({ topic: NOTIFICATION_TOPIC, notification: { title, body }, data: { @@ -145,9 +157,10 @@ async function sendAutomationNotification({ title, body, type, data = {} }) { }, }, }); - console.log(`🔔 Notification sent: ${type}`); + console.log(`🔔 FCM Notification sent: ${type} (ID: ${result})`); } catch (error) { - console.error('❌ Failed to send notification:', error.message); + // Don't treat as error - app will handle notifications via Firebase listeners + console.log(`📝 [INFO] FCM not available (${error.message}). App will use local notifications via Firebase listener.`); } }