103 lines
2.0 KiB
JavaScript
103 lines
2.0 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, type, logo,id } = req.body;
|
|
|
|
|
|
let message = {};
|
|
if (type === "text") {
|
|
|
|
message = {
|
|
notification: {
|
|
title: title,
|
|
body: body,
|
|
},
|
|
data: {
|
|
title: title,
|
|
body: body,
|
|
temp_source: "text",
|
|
route: "splaasa",
|
|
id: id,
|
|
},
|
|
token: token,
|
|
};
|
|
|
|
}
|
|
|
|
else if (type === "image") {
|
|
|
|
message = {
|
|
notification: {
|
|
title: title,
|
|
body: body,
|
|
imageUrl: logo,
|
|
},
|
|
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");
|
|
}); |