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

188
Views
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
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!