multiple fcmIds added

master^2
Bhaskar 9 months ago
parent a11f695867
commit e35296c0a3

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

@ -166,16 +166,21 @@ fastify.post("/api/login", {
properties: { properties: {
phone: { type: "string" }, phone: { type: "string" },
password: { type: "string" }, password: { type: "string" },
fcmId: { type: "string" }, // Add this line // fcmId: { type: "string" }, // Add this line
fcmIds: {
type: "array", // Change this to allow an array
items: { type: "string" }, // Each item in the array is a string
default: [], // Default value if not provided
},
deviceId: { type: "string" } // Add this line deviceId: { type: "string" } // Add this line
}, },
}, },
}, },
async handler(req, reply) { async handler(req, reply) {
// Pass fcmId and deviceId to the loginUser function // Pass fcmId and deviceId to the loginUser function
const { phone, password, fcmId, deviceId } = req.body; const { phone, password, fcmIds, deviceId } = req.body;
console.log(password,phone) console.log(password,phone)
const loginObject = await userController.loginUser(req, fcmId, deviceId); const loginObject = await userController.loginUser(req, fcmIds, deviceId);
if (loginObject.same) { if (loginObject.same) {
console.log("entered 1st loop") console.log("entered 1st loop")
@ -187,6 +192,12 @@ fastify.post("/api/login", {
typeof oneTimePasswordSetFlag, typeof oneTimePasswordSetFlag,
typeof phoneVerified typeof phoneVerified
); );
if (fcmIds && fcmIds.length > 0) {
await User.updateOne(
{ customerId: loginObject.user.customerId },
{ $addToSet: { fcmIds: { $each: fcmIds } } } // Add multiple FCM IDs, avoiding duplicates
);
}
if (!phoneVerified) { if (!phoneVerified) {
reply.send({ reply.send({
simplydata: { simplydata: {
@ -366,7 +377,7 @@ console.log(user)
phoneVerified: user.phoneVerified, phoneVerified: user.phoneVerified,
oneTimePasswordSetFlag: user.oneTimePasswordSetFlag, oneTimePasswordSetFlag: user.oneTimePasswordSetFlag,
type: user.profile.role, type: user.profile.role,
fcmId: user.fcmId, fcmIds: user.fcmIds,
team: user.team, team: user.team,
city: user.city, city: user.city,
manager: user.manager, manager: user.manager,
@ -899,7 +910,7 @@ fastify.post("/api/insatllLogin", {
phoneVerified: install.phoneVerified, phoneVerified: install.phoneVerified,
oneTimePasswordSetFlag: install.oneTimePasswordSetFlag, oneTimePasswordSetFlag: install.oneTimePasswordSetFlag,
type: install.profile.role, type: install.profile.role,
fcmId: install.fcmId, fcmIds: install.fcmIds,
team: install.team, team: install.team,
city: install.city, city: install.city,
manager: install.manager, manager: install.manager,

@ -118,7 +118,9 @@ const userSchema = new mongoose.Schema(
latitude: {type: Number,default: 0.0}, latitude: {type: Number,default: 0.0},
isActive: Boolean, isActive: Boolean,
tenantId: ObjectId, tenantId: ObjectId,
fcmId: { type: String, default: null }, // fcmId: { type: String, default: null },
fcmIds: [{ type: String }], // Changed to an array of strings
deviceId: { type: String, default: null }, deviceId: { type: String, default: null },
createdAt: { createdAt: {
type: Date, type: Date,

Loading…
Cancel
Save