Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda