I have this code in a cloud function
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
const admin = require('firebase-admin')
admin.initializeApp()
const db = getDatabase()
exports.customProgramNotification = async (req, res) => {
let tokenFCM
let programCreator = req.body.createdFrom
let programLink = `http://example.com/custom-programs/${req.body.programID}`
const ref = db.ref('Users')
ref('value', (data) => {
data.forEach( (user) => {
let userData = user.val()
if( userData.uid === req.body.uid ) {
if(userData.hasOwnProperty('tokenFCM')){
tokenFCM = userData.tokenFCM
} else {
res.send('Token FCM not available')
}
}
})
})
// Notification details.
const payload = {
notification: {
title: 'New content available',
body: `The content created by ${programCreator} is available at the following link: ${programLink}`,
//icon: follower.photoURL
}
};
// Listing all tokens as an array.
// Send notifications to all tokens.
const response = await admin.messaging().sendToDevice(tokenFCM, payload);
res.status(200).send(response);
};
I need to send a custom notification to a specific user when a content is created from another user.
I'm testing the code but I'm unable to depploy the function due to an error. I've inspected the log and this is the error message
Function failed on loading user code. This is likely due to a bug in the user code.
I've never used firebase admin sdk and I suppose that it's the same of firebase client.
What I'm trying to do is to check the database for a given user id that is passed into the POST request and if it's found and a FCM token is available, take the token and then send the notification that will include a link.
Is there something wrong in my code and how I fix it?