Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda