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.