Skip to content

Commit

Permalink
Code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
kaveeshadinamidu committed Nov 19, 2022
1 parent 7d6540c commit 4d19583
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/services/notificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,41 @@ const createOutput = require("../helpers/createOutput");
var serviceAccount = require("./firebase-conf.json");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
credential: admin.credential.cert(serviceAccount),
});


function sendNotifications(tokens){
function sendNotifications(tokens) {
const message = {
notification :{
title:"Intrusion Detected",
body:"An intrusion detected in your cctv system",

notification: {
title: "Intrusion Detected",
body: "An intrusion detected in your cctv system",
},
tokens:tokens,
}
return new Promise((resolve,reject)=>{
admin.messaging().sendMulticast(message).then((response)=>{
console.log(response);
if (response.failureCount > 0) {
const failedTokens = [];
response.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push(tokens[idx]);
}
});
resolve(createOutput(400,"Error occured while sending to some devices"));

}else{
resolve(createOutput(200,"Notifications sended succesfully!"));
}
}).catch((e) =>{
reject(e);
});
tokens: tokens,
};
return new Promise((resolve, reject) => {
admin
.messaging()
.sendMulticast(message)
.then((response) => {
console.log(response);
if (response.failureCount > 0) {
const failedTokens = [];
response.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push(tokens[idx]);
}
});
resolve(
createOutput(400, "Error occured while sending to some devices")
);
} else {
resolve(createOutput(200, "Notifications sended succesfully!"));
}
})
.catch((e) => {
reject(e);
});
});

}

module.exports = {sendNotifications}
module.exports = { sendNotifications };

0 comments on commit 4d19583

Please sign in to comment.