ashok 9 months ago
commit feaf9277c6

@ -1714,33 +1714,53 @@ const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => {
// } // }
// }; // };
const sendNotification = async (fcmTokens, title, body) => { const sendNotification = async (fcmIds, title, body) => {
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) { try {
console.error('No FCM tokens provided.'); if (!fcmIds || fcmIds.length === 0) {
return; throw new Error('No FCM tokens provided.');
} }
const message = { // Flatten the nested array of FCM tokens
tokens: fcmTokens, // Send to multiple tokens const flatTokens = fcmIds.flat();
notification: {
title: title,
body: body,
},
data: {
target: 'tank_levels', // Any additional data you want to send
},
};
try { if (flatTokens.length === 0) {
const response = await admin.messaging().sendMulticast(message); // Send all messages at once throw new Error('Flattened FCM token list is empty.');
console.log('Notification sent successfully:', response); }
// Check for failures
if (response.failureCount > 0) { const message = {
response.responses.forEach((resp, idx) => { notification: {
if (!resp.success) { title,
console.error(`Failed to send notification to token ${fcmTokens[idx]}:`, resp.error); 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) { } catch (error) {
console.error('Error sending notifications:', error); console.error('Error sending notifications:', error);
@ -1748,6 +1768,9 @@ const sendNotification = async (fcmTokens, title, body) => {
}; };
// const sendPushNotification = async (registrationToken, title, body) => { // const sendPushNotification = async (registrationToken, title, body) => {
// const message = { // const message = {
// notification: { // notification: {
@ -2095,7 +2118,7 @@ exports.motorAction = async (req, reply) => {
// Get user FCM tokens // Get user FCM tokens
const users = await User.find({ customerId }); const users = await User.find({ customerId });
const fcmToken = users.map(user => user.fcmIds).filter(fcmIds => fcmIds); 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() }); const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
console.log(receiverTank) console.log(receiverTank)
const currentWaterLevel = parseInt(receiverTank.waterlevel, 10); const currentWaterLevel = parseInt(receiverTank.waterlevel, 10);

Loading…
Cancel
Save