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

182
Views
How I can get a specific value of Firebase Real time database using Firebase Cloud Functions

I want to access this Firebase token (androidNotificationToken) that is store in Firebase real time database as show below:

  "2KdtpuixaWTWH7J4fvqoVanb5Pj2" : {

    "androidNotificationToken" : "eiaSIC3STOuDv7d_HX-BC0:APA91bExRMsOPPzSzvIQWus4WPNEyCEcQ6ENSVRw8xQhG52CXbaEJLRQWek9bjiQAsl_WnPxdoxoS0-j82bNClIxg1XSVxKpfXtXoPuGunCQgRk99CR_sHnaWqHpUCIfnDjAF7lQcx8b",

    "firstName" : "Jhon",

    "ownerId" : "2KdtpuixaWTWH7J4fvqoVanb5ddPj2",
    
  },

  "3DuDYccJAMUgF7fFpAQOKoKsRt02" : {

    "androidNotificationToken" : "cZbYu5neSuab3R-b5Vj3Zw:APA91bEgGRrb4MVhaqEg_IU--4ueeohkjMijRWbcqLQl0feSOx6oTPICurj8N6ftYl9OzDZS7MVtJsVQHfqPFPMeepkaqvAfBxX2WNCpnECUWq-vSq1nPTmUpKp1spQvPBmUNfy8CMHI",

    "firstName" : "Tom",

    "ownerId" : "3DuDYccJAMUgF7fFpAQerOKoKsRt02",

  },
}

This code I have to use to access this toke through firebase function is:

exports.onCreateFollower = functions.database.ref('switchFollowing/{userId}/{followingId}')
    .onCreate( async (snapshot, context) => {

const CreatedActivityFeedItem = snapshot.val()
const userId  = CreatedActivityFeedItem.followingId
const androidNotificationToken = CreatedActivityFeedItem.token;
const token;

const userRef = firebase.database().ref('/{userId}');

userRef.once('value').then(function(snapshot) {

 token = snapshot.val().androidNotificationToken;
 
 console.log('Token0', token);


}

     console.log('Token0', token);
     console.log('followerId0', userId);


  if(androidNotificationToken){

    sendNotification(androidNotificationToken, CreatedActivityFeedItem);

    }else{

    console.log("no Token For User, can not send notification");

    }
  function sendNotification(androidNotificationToken,activityFeedItem ){

    let body;

  body = "Someone follow You";

    const message =  {
  notification : {
  title: "Switch Alert!",
  body},
  token: androidNotificationToken,
  data:  {recipient: userId}

    };


    admin
    .messaging()
    .send(message)
    .then(response => {
      // Response is a message ID string
   return   console.log("Successfully sent message", response);
    })
    .catch(error => {
      console.log("Error sending message", error);
    });


  }



    });

I have tried to follow a lot of answers and documentation of firebase but nothing help. I have a very little knowledge of java script that's why I find it difficult. Moreover, I am able to get "userId" value but failed to get "token" value and want to use it in "exports.onCreateFollower" function. I think this code is enough to understand. Please ask me if any thing else required! :)

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!