|
|
@@ -1,14 +1,16 @@ |
|
|
|
const express = require("express"); |
|
|
|
require('dotenv').config(); |
|
|
|
const path = require("path"); |
|
|
|
var admin = require("firebase-admin"); |
|
|
|
const port = process.env.PORT || 14003; |
|
|
|
|
|
|
|
var dirPath = path.join(__dirname, "fcm_key"); |
|
|
|
|
|
|
|
var keyPath = require("./keypath.json"); |
|
|
|
var data = require("./keypath.json"); |
|
|
|
const { messaging } = require("firebase-admin"); |
|
|
|
|
|
|
|
for (key in keyPath) { |
|
|
|
var data = keyPath[key]; |
|
|
|
//dijadiin 1 ThamrinGroup dan HRIS |
|
|
|
// for (key in keyPath) { |
|
|
|
var jsonPath = path.join(dirPath, data.PATH); |
|
|
|
var adminAuthToken = require(jsonPath); |
|
|
|
admin.initializeApp( |
|
|
@@ -16,9 +18,9 @@ for (key in keyPath) { |
|
|
|
credential: admin.credential.cert(adminAuthToken), |
|
|
|
databaseURL: data.DB_URL, |
|
|
|
}, |
|
|
|
key |
|
|
|
// key |
|
|
|
); |
|
|
|
} |
|
|
|
// } |
|
|
|
|
|
|
|
const app = express(); |
|
|
|
|
|
|
@@ -32,25 +34,54 @@ app.use( |
|
|
|
app.post("/sendFcm", async function (req, res) { |
|
|
|
try { |
|
|
|
var message = req.body; |
|
|
|
var fcm = admin.app(application.application); |
|
|
|
|
|
|
|
fcm |
|
|
|
.messaging() |
|
|
|
.sendMulticast(message) |
|
|
|
console.log(message); |
|
|
|
if(message){ |
|
|
|
messaging().sendMulticast(message) |
|
|
|
.then((response) => { |
|
|
|
// console.log(response.successCount + ' messages were sent successfully'); |
|
|
|
if (response.failureCount > 0) { |
|
|
|
const failedTokens = []; |
|
|
|
response.responses.forEach((resp, idx) => { |
|
|
|
if (!resp.success) { |
|
|
|
failedTokens.push(registrationTokens[idx]); |
|
|
|
} |
|
|
|
}); |
|
|
|
if(message.tokens){ |
|
|
|
const failedTokens = []; |
|
|
|
response.responses.forEach((resp, idx) => { |
|
|
|
if (!resp.success) { |
|
|
|
failedTokens.push(message.tokens[idx]); |
|
|
|
} |
|
|
|
}); |
|
|
|
console.log("Failed Fcm Notif",failedTokens); |
|
|
|
return res.status(400).json(response); |
|
|
|
} |
|
|
|
else{ |
|
|
|
return res.status(400).json({error:"Error sending FCM Notification!"}); |
|
|
|
} |
|
|
|
} |
|
|
|
return res.status(200).json(response); |
|
|
|
}) |
|
|
|
.catch((error) => { |
|
|
|
}).catch((error) => { |
|
|
|
return res.status(404).json(error); |
|
|
|
}); |
|
|
|
} |
|
|
|
else{ |
|
|
|
return res.status(404).json({error:"Message can't be empty!"}); |
|
|
|
} |
|
|
|
|
|
|
|
// 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); |
|
|
|