From d5426f95867a4f3b45fb16a85399cf980ea3f10d Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 15 Jan 2025 12:15:29 +0530 Subject: [PATCH] notifications chnages --- src/controllers/tanksController.js | 73 ++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0daace6c..fe5a0aab 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1714,33 +1714,53 @@ const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => { // } // }; -const sendNotification = async (fcmTokens, title, body) => { - if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) { - console.error('No FCM tokens provided.'); - return; - } +const sendNotification = async (fcmIds, title, body) => { + try { + if (!fcmIds || fcmIds.length === 0) { + throw new Error('No FCM tokens provided.'); + } - const message = { - tokens: fcmTokens, // Send to multiple tokens - notification: { - title: title, - body: body, - }, - data: { - target: 'tank_levels', // Any additional data you want to send - }, - }; + // Flatten the nested array of FCM tokens + const flatTokens = fcmIds.flat(); - try { - const response = await admin.messaging().sendMulticast(message); // Send all messages at once - console.log('Notification sent successfully:', response); - // Check for failures - if (response.failureCount > 0) { - response.responses.forEach((resp, idx) => { - if (!resp.success) { - console.error(`Failed to send notification to token ${fcmTokens[idx]}:`, resp.error); - } + if (flatTokens.length === 0) { + throw new Error('Flattened FCM token list is empty.'); + } + + const message = { + notification: { + title, + body, + }, + data: { + exampleKey: 'exampleValue', // Optional additional data + }, + }; + + if (flatTokens.length === 1) { + // Single token case + const response = await admin.messaging().send({ + ...message, + token: flatTokens[0], }); + console.log('Notification sent successfully to single token:', response); + } else { + // Multiple tokens case + const response = await admin.messaging().sendMulticast({ + ...message, + tokens: flatTokens, + }); + + console.log('Notifications sent successfully:', response); + + // Handle failures + if (response.failureCount > 0) { + response.responses.forEach((resp, idx) => { + if (!resp.success) { + console.error(`Failed to send notification to token ${flatTokens[idx]}:`, resp.error); + } + }); + } } } catch (error) { console.error('Error sending notifications:', error); @@ -1748,6 +1768,9 @@ const sendNotification = async (fcmTokens, title, body) => { }; + + + // const sendPushNotification = async (registrationToken, title, body) => { // const message = { // notification: { @@ -2095,7 +2118,7 @@ exports.motorAction = async (req, reply) => { // Get user FCM tokens const users = await User.find({ customerId }); const fcmToken = users.map(user => user.fcmIds).filter(fcmIds => fcmIds); - +console.log(fcmToken) const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); console.log(receiverTank) const currentWaterLevel = parseInt(receiverTank.waterlevel, 10);