|
|
|
@ -1569,7 +1569,7 @@ eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel,
|
|
|
|
|
// await sendNotification(fcmTokens, 'Motor Started', message);
|
|
|
|
|
try {
|
|
|
|
|
// Retrieve the user information
|
|
|
|
|
const users = await User.find({ fcmId: { $in: fcmTokens } });
|
|
|
|
|
const users = await User.find({ fcmIds: { $in: fcmTokens } });
|
|
|
|
|
console.log("users",users)
|
|
|
|
|
const userNames = users.map(user => user.username).join(', ');
|
|
|
|
|
console.log("userNames",userNames)
|
|
|
|
@ -1592,7 +1592,7 @@ eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel,
|
|
|
|
|
eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => {
|
|
|
|
|
try {
|
|
|
|
|
// Retrieve the user information
|
|
|
|
|
const users = await User.find({ fcmId: { $in: fcmTokens } });
|
|
|
|
|
const users = await User.find({ fcmIds: { $in: fcmTokens } });
|
|
|
|
|
console.log("users",users)
|
|
|
|
|
const userNames = users.map(user => user.username).join(', ');
|
|
|
|
|
console.log("userNames",userNames)
|
|
|
|
@ -1687,35 +1687,67 @@ 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;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// for (const token of fcmTokens) {
|
|
|
|
|
// const message = {
|
|
|
|
|
// token: token,
|
|
|
|
|
// notification: {
|
|
|
|
|
// title: title,
|
|
|
|
|
// body: body,
|
|
|
|
|
// },
|
|
|
|
|
// data: {
|
|
|
|
|
// target: 'tank_levels',
|
|
|
|
|
// },
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// const response = await admin.messaging().send(message); // Send each message individually
|
|
|
|
|
// console.log('Notification sent successfully:', response);
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error(`Failed to send notification to token ${token}:`, error);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
const sendNotification = async (fcmTokens, title, body) => {
|
|
|
|
|
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
|
|
|
|
|
console.error('No FCM tokens provided.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const token of fcmTokens) {
|
|
|
|
|
const message = {
|
|
|
|
|
token: token,
|
|
|
|
|
tokens: fcmTokens, // Send to multiple tokens
|
|
|
|
|
notification: {
|
|
|
|
|
title: title,
|
|
|
|
|
body: body,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
target: 'tank_levels',
|
|
|
|
|
target: 'tank_levels', // Any additional data you want to send
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await admin.messaging().send(message); // Send each message individually
|
|
|
|
|
const response = await admin.messaging().sendMulticast(message); // Send all messages at once
|
|
|
|
|
console.log('Notification sent successfully:', response);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`Failed to send notification to token ${token}:`, error);
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error sending notifications:', error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const sendPushNotification = async (registrationToken, title, body) => {
|
|
|
|
|
// const message = {
|
|
|
|
|
// notification: {
|
|
|
|
@ -2062,7 +2094,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
// Get user FCM tokens
|
|
|
|
|
const users = await User.find({ customerId });
|
|
|
|
|
const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId);
|
|
|
|
|
const fcmToken = users.map(user => user.fcmIds).filter(fcmIds => fcmIds);
|
|
|
|
|
|
|
|
|
|
const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
|
|
|
|
|
console.log(receiverTank)
|
|
|
|
|