Skip to content

Instantly share code, notes, and snippets.

@vaibhavgehani
Created September 4, 2022 05:24
Show Gist options
  • Save vaibhavgehani/2c1294965acd754030bcfce4b537e28a to your computer and use it in GitHub Desktop.
Save vaibhavgehani/2c1294965acd754030bcfce4b537e28a to your computer and use it in GitHub Desktop.
const express = require("express");
const app = express();
const bodyParser = require('body-parser');
const admin = require('firebase-admin');
const serviceAccount = require("./serviceAccountKey.json");
var jsonParser = bodyParser.json()
app.use(bodyParser.urlencoded({
extended:true
}));
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
app.post('/sendChatNotification', jsonParser, async (req, res) => {
const notificationInfo = req.body;
// Firebase push token is saved in Firestore
const user_doc = await admin.firestore().doc(`ChatUsers/` + notificationInfo.reciver_info).get();
if (user_doc.exists) {
const userData = user_doc.data();
// reciver push token
if (userData.pushToken) {
// updating icon badge number
const badgeNumber = userData.notificationCount + 1;
// updating badge number in Firestore document
admin.firestore().doc(`ChatUsers/` + notificationInfo.reciver_info).update({notificationCount: badgeNumber});
await push.sendChatAppMessage([userData.pushToken], badgeNumber).then((messageRes) => {
res.send({
error: false,
message: 'OK'
});
});
} else {
res.send({
error: false,
message: 'No Push token'
})
}
} else {
res.send({
error: true,
message: 'No such user'
})
}
})
app.listen(route, () => {
console.log("Server Started at", process.env.PORT || 3000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment