I want to implement push notifications automatically and I've used javascript (node.js) but I got this error
Function returned undefined, expected Promise or value
I am not node js developer I am a flutter developer and I don't know what is promises.
this is my code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var notificationMessageData;
exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot , context) => {
notificationMessageData = snapshot.data();
admin.firestore().collection('pushTokens').get().then(async (snapshot) => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": "from" + notificationMessageData.writer,
"body": "from" + notificationMessageData.name,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.writer,
"message": notificationMessageData.name
}
}
return await admin.messaging().sendToDevice(tokens , payload).then((response) => {
console.log('nice');
}).catch((err) => {
console.log(err);
})
}
})
})
Everything is going okay and I upload it without any problem but when adding a document to the posts collection it outputs in the logs the above error.
I have created a user registration form and I've registered the users and put their token id in a collection called pushTokens and then sending a notification for each user inside that collection but this didn't work.
