How to get that token_id in node js?
database image
Index.js code is below, by this code it gives all the data stored in user_id but i'm unable to get only that particular field of {token_id}.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
exports.sendoNotification = functions.firestore.document('/Users/{user_id}/Notification/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification from : ', user_id);
var cityRef = db.collection('Users').doc(user_id);
var getDoc = cityRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
return Promise.all([getDoc]).then(result => {
const tokenId = result[0].data().token_id;
const notificationContent = {
notification: {
title:"notification",
body: "friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
console.log("Notification sent!");
});
});
});
});
