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

341
Views
How to get a user's fcm token from Firestore?

I'm struggling getting fcm messages to send and getting the error

Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.

I am using firebase functions to send the messages. Here is the part of the function that defines the token. In the log, the token is efB_l9RCMEIErl8m...

exports.newUpdateReqText = functions.firestore
  .document('texts/{id}').onCreate(snap => {
    const afterData = snap.data()
    let name = afterData.name;
    let phone = afterData.phone.replace(/[^0-9]+/g, "");
    let created = afterData.created;
    let userId = afterData.userId;

    return admin.firestore()
      .collection('users')
      .doc(userId)
      .get()
      .then(doc => {
        let token = doc.data().fcm_token
        functions.logger.log(token)

        const payload = {
          notification: {
            title: 'Please update your profile',
            message: `Hi ${name}! It looks like your profile isn't complete. Please provide the rest of the required information ASAP. https://mvpeventstaffing.com/profile.`,
          },
          data: {
            "title": 'Please update your profile',
            "body": `Hi ${name}! It looks like your profile isn't complete. Please provide the rest of the required information ASAP. https://mvpeventstaffing.com/profile.`,
            "action_item": "venue",
            "action_id": "1",
            "actionable": "true",
            "priority": "high",
          },
        }

        return admin.messaging().sendToDevice(token, payload)
        .then((response)=> {
        return console.log("Successfully sent message:", response);
        })
        .catch((error) =>{
        console.log("Error sending message:", error);
        });
      })

Reading answers to similar questions on this site, they commonly respond that the way to get the token is this:

 const getdevicetokenpromise = db.ref(`/DriversAvailable/${key}/token`).once('value');

But, from what I can tell, that is with realtime db, not firestore. I get db.ref is not a function when I tried to write it like this.

So is my data being stored the wrong way? Am I accessing it the wrong way? Or is my function to send the message wrong? Any help would be appreciated.

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!