|
- const express = require("express");
- const path = require("path");
- var admin = require("firebase-admin");
- const port = process.env.PORT || 3004;
-
- var dirPath = path.join(__dirname, "fcm_key");
-
- var keyPath = require("./keypath.json");
-
- for (key in keyPath) {
- var data = keyPath[key];
- var jsonPath = path.join(dirPath, data.PATH);
- var adminAuthToken = require(jsonPath);
- admin.initializeApp(
- {
- credential: admin.credential.cert(adminAuthToken),
- databaseURL: data.DB_URL,
- },
- key
- );
- }
-
- const app = express();
-
- app.use(
- express.json({ limit: "50mb", parameterLimit: 1000000, extended: true })
- );
- app.use(
- express.urlencoded({ limit: "50mb", parameterLimit: 1000000, extended: true })
- );
-
- app.post("/sendFcm", async function (req, res) {
- try {
- var message = req.body;
- var fcm = admin.app(application.application);
-
- fcm
- .messaging()
- .sendMulticast(message)
- .then((response) => {
- if (response.failureCount > 0) {
- const failedTokens = [];
- response.responses.forEach((resp, idx) => {
- if (!resp.success) {
- failedTokens.push(registrationTokens[idx]);
- }
- });
- }
- return res.status(200).json(response);
- })
- .catch((error) => {
- return res.status(404).json(error);
- });
- } catch (e) {
- console.log(e.message);
- return res.status(404).json(e.message);
- }
- });
-
- app.listen(port, () => console.log(`Listening into port ${port}`));
|