0
votes

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.

1
After you get the token with this statement, const token_id = result.val(), add a console.log() statement to output user_id and token_id. Most likely, the token_id is null for some user. The log output will show you which one it is.Bob Snyder
Okay. I found a silly mistake of mine and on correction it started working fine. Thanks for ur reply.Puneet Tawniya
Can u please tell me what was the mistake as I am also facing a similar issue.Gurjeet Singh

1 Answers

2
votes

You haven't use a templete tag ${} at certain places

Also make sure there is a valid device_token stored in firebase for the user you're sending the notification.

The final code after implementing these changes and updating methods would be:

'use strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {

  const user_id = context.params.user_id;
  const notification_id = context.params.notification_id;

  console.log('We have a notification to send to : ', context.params.user_id);

  if(!change.after.val()) {
      return console.log('A Notification has been deleted from the database : ' + context.params.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 => {
        return console.log('This was the notification Feature');  
    });

  });

});

To everyone referring to the TVAC Studio's tutorial of Lapit Chat App, I referred to this answer for the above changes https://stackoverflow.com/a/49746976/7549743