Files
notification/index.js
T
2026-05-13 12:10:46 +05:30

108 lines
2.1 KiB
JavaScript

const express = require("express");
const admin = require("firebase-admin");
const serviceAccount = require("./performicsone-app-firebase-adminsdk-usel3-dc4337e74b.json");
const app = express();
app.use(express.json());
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
// let token = "cFB2wzm9RVqfkNJgtdhGK3:APA91bFnNiuLRk62V9mdtwUki3hpM0Th4aoPV2zsXD_8IOCXtnUJrrKF43Olk95SFZEamgewsTbhhqS2GWkji4o8GEzmnTsUoSS_SJAgq_i8HjzKe9nCD90";
// let title = "Hello World";
// let body = "This is a test notification";
app.post("/send-notification", async (req, res) => {
try {
const { token, title, body, logo, MediaType, TargetScreen } = req.body;
let message = {};
if (MediaType === "text") {
message = {
notification: {
title: title,
body: body,
},
data: {
title: title,
body: body,
temp_source: "text",
route: TargetScreen,
},
token: token,
};
}
else if (MediaType === "image") {
message = {
notification: {
title: title,
body: body,
imageUrl: logo,
},
data: {
title: title,
body: body,
temp_source: "image",
route: TargetScreen,
},
android: {
notification: {
imageUrl: logo,
},
},
apns: {
payload: {
aps: {
"mutable-content": 1,
},
},
fcm_options: {
image: logo,
},
},
token: token,
};
}
else {
return res.status(400).json({
success: false,
message: "Invalid type",
});
}
const response = await admin.messaging().send(message);
return res.status(200).json({
success: true,
message: "Notification sent successfully",
response: response,
});
} catch (error) {
console.log(error);
return res.status(500).json({
success: false,
error: error.message,
});
}
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});