Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Cloud Functions - Unable to deploy function to send push notifications

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!