notifications chnages

master^2
Bhaskar 9 months ago
parent e35296c0a3
commit d5426f9586

@ -1714,40 +1714,63 @@ 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.');
}
// Flatten the nested array of FCM tokens
const flatTokens = fcmIds.flat();
if (flatTokens.length === 0) {
throw new Error('Flattened FCM token list is empty.');
} }
const message = { const message = {
tokens: fcmTokens, // Send to multiple tokens
notification: { notification: {
title: title, title,
body: body, body,
}, },
data: { data: {
target: 'tank_levels', // Any additional data you want to send exampleKey: 'exampleValue', // Optional additional data
}, },
}; };
try { if (flatTokens.length === 1) {
const response = await admin.messaging().sendMulticast(message); // Send all messages at once // Single token case
console.log('Notification sent successfully:', response); const response = await admin.messaging().send({
// Check for failures ...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) { if (response.failureCount > 0) {
response.responses.forEach((resp, idx) => { response.responses.forEach((resp, idx) => {
if (!resp.success) { if (!resp.success) {
console.error(`Failed to send notification to token ${fcmTokens[idx]}:`, resp.error); 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);
} }
}; };
// 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