Files
bodyshop/server/firebase/firebase-handler.js

48 lines
1.3 KiB
JavaScript

var admin = require("firebase-admin");
const path = require("path");
require("dotenv").config({ path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || 'development'}`) });
console.log(
"Found Firebase AdminSDK JSON",
process.env.NODE_ENV,
!!process.env.FIREBASE_ADMINSDK_JSON
);
var serviceAccount = require(process.env.FIREBASE_ADMINSDK_JSON);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
exports.admin = admin;
exports.sendNotification = (req, res) => {
console.log("Firebase Send.");
var registrationToken =
"fqIWg8ENDFyrRrMWJ1sItR:APA91bHirdZ05Zo66flMlvala97SMXoiQGwP4oCvMwd-vVrSauD_WoNim3kXHGqyP-bzENjkXwA5icyUAReFbeHn6dIaPcbpcsXuY73-eJAXvZiu1gIsrd1BOsnj3dEMT7Q4F6mTPth1";
var message = {
notification: { title: "The Title", body: "The Body" },
data: {
jobid: "1234",
},
token: registrationToken,
};
// Send a message to the device corresponding to the provided
// registration token.
admin
.messaging()
.send(message)
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message:", error);
});
res.sendStatus(200);
};