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

189
Visualizações
Sending content from snapshot as push notification

I want to send the user a push notification with cloud functions on Firebase if someone commented to a topic on his iOS Device. This code below works fine if I put the content of the notification manually.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

   exports.Push = functions.database.ref('/placeID/{pushId}/')
   .onCreate((snapshot, context) => {
 

      var topic = 'weather';
      const payload = {
          notification: {
              title: 'User Max',
              body: 'Hi how are you?',
              badge: '1',
              sound: 'default'
          }
     };
  
     admin.messaging().sendToTopic(topic,payload);

   })

In the next step I want to get the content of the comment the person has sent. Kinda get the value of the snapshot. I tried it like this and get no error in the Logs Explorer from Google. But also no Push Notification anymore.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

   exports.Push = functions.database.ref('/placeID/{autogeneratedplaceID}/')
   .onCreate((snapshot, context) => {
 
      var username = snapshot.child("userName").val()
      var usercomment = snapshot.child("userComment").val()

      var topic = 'weather';
      const payload = {
          notification: {
              title: username,
              body: usercomment,
              badge: '1',
              sound: 'default'
          }
     };
     admin.messaging().sendToTopic(topic,payload);
   })

What is wrong here? I also found a video on Youtube with the exact solution. But this is for android and it did not really help me. Youtube

This is how my firebase realtime database structure looks like. enter image description here

about 4 years ago · Santiago Gelvez
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