I just got my hands on Firebase Functions and initially, everything was going correct, but now I am facing with the following Error. I am providing error and my codes below.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.sendNotification = functions.database.ref('/Notification/{user_id}/{notification_id}').onWrite(event =>{
const user_id = event.params.user_id;
const Notification = event.params.Notification;
console.log('We have a Notification to send to :', user_id);
if (!event.data.val()) {
return console.console.log('A Notify has been deleted from the database :', notification_id);
}
const devicetoken = admin.database().ref(`/Users/{user_id}/device_token`).once('value');
return devicetoken.then(result =>{
const token_id = result.val();
const payload = {
notification: {
title : "Follower Request",
body: "You've received a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response =>{
console.log('This was the notification Feature')
});
});
});
Below is the error which I am receiving in Firebase functions.
Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array. at FirebaseMessagingError.Error (native) at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:207:16) at Messaging.validateRegistrationTokensType (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:589:19) at Messaging.sendToDevice (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:210:14) at devicetoken.then.result (/user_code/index.js:36:30) at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Image of firebase function for above error
Image for providing an idea of how I have stored device_token_id
Any help will be much appreciated.
const token_id = result.val()
, add aconsole.log(
) statement to outputuser_id
andtoken_id
. Most likely, thetoken_id
is null for some user. The log output will show you which one it is. – Bob Snyder