🔧 Fix: Handle FCM messaging gracefully - fallback when not available

This commit is contained in:
Wizznu 2026-04-10 18:17:56 +07:00
parent e04763fe8e
commit f943bd9b7c
1 changed files with 16 additions and 3 deletions

View File

@ -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.`);
}
}