javascript – Firebase Funtion For FCM Does Not works. Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array-ThrowExceptions

Exception or error:

I have two scenarios where i want to send notification once to:

1] Admin : When Order is placed;
2] User : Status Updates of Order;

I have two separate functions for both operations.

The FCM Cloud Function for Admin works perfectly. But the FCM Function for User does not. Even though the Functions are written the same way just the DB Paths are different.

FUNCTION FOR ADMIN

'use strict'

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


exports.sendNotification = functions.database.ref('/orders/{orderId}/order_placed_by_name').onWrite((data, context) => {
    const orderId = context.params.orderId;
    const order_placed_by_name = context.params.order_placed_by_name;

    if(!data.after.val()){
        return null;
    }else{

    }

    const userName = admin.database().ref(`/orders/${orderId}/order_placed_by_name`).once('value');
    const userNo = admin.database().ref(`/orders/${orderId}/order_placed_by_no`).once('value');
    const adminFCMToken = admin.database().ref(`/admin/fcmtoken`).once('value');

    var cusName;
    var adminFCM;
    var orderNumber = orderId;

    userName.then(result => {
        cusName = result.val();
    });
    userNo.then(result => {
        cusNo = result.val();
    });

    return adminFCMToken.then(result => {
        adminFCM = result.val();

        const payload = {
            notification:{
                title: 'New Order Placed',
                body: `New Order Placed By: ${cusName} and Order No is: ${orderNumber}`,
                icon: "default"
            }
        };

        return admin.messaging().sendToDevice(adminFCM, payload).then(response => {
            console.log('Delivered');
        });
    });
});

FUNCTION FOR USER

'use strict'

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

exports.sendNotificationToUser = functions.database.ref('/orders/{orderId}/order_remark').onWrite((data, context) => {
    const orderId = context.params.orderId;

    if(!data.after.val()){
        return null;
    }else{

    }

    const remarkInstance = admin.database().ref(`/orders/${orderId}/order_remark`).once('value');
    const userNo = admin.database().ref(`/orders/${orderId}/order_placed_by_no`).once('value');
    const userFCMToken = admin.database().ref(`/Users/${cusNo}/fcmtoken`).once('value');    

    var remark;
    var cusNo;
    var userFCM;

    remarkInstance.then(result1 => {
        remark = result1.val();
    });

    userNo.then(result2 => {
        cusNo = result2.val();
    });

    var msg;

    console.log(remark);

    if(remark > 2){
        switch(remark){
        case 3:
            msg = "Order is out for delivery";
            break;
        case 4:
            msg = "Order Cancelled by Admin";
            break;    
        case 5:
            msg = "Order Delivered Successfully";
            break;  
        case 6:
            msg = "Order can not be delivered by delivery boy";
            break;          
        }
    }

    return userFCMToken.then(result3 => {
        userFCM = result3.val();

        console.log(userFCM);

        const payload = {
            notification:{
                title: 'Order Update',
                body: msg + " ",
                icon: "default"
            }
        };

        return admin.messaging().sendToDevice(userFCM, payload).then(response => {
            console.log('Delivered');
        });
    });
});

DATABASE STRUCTURE:

{
  "Users" : {
    "+916260822286" : {
      "account_number" : "8878878878",
      "account_status" : 1,
      "cancelled_orders" : 0,
      "fcmtoken" : "ABC",
      "user_name" : "Manish"
    }
  },
  "admin" : {
    "adminMobileNumber" : "6262626262",
    "adminPassword" : "pass",
    "fcmtoken" : "TOKEN",
  },
  "orders" : [ {
    "city" : "बुरहानपुर",
    "colony_name" : "wuehe",
    "house_number" : " sejwu",
    "orderLocation" : {
      "latitude" : 11.835304615745486,
      "longitude" : 16.35319210588932
    },
    "order_cost" : "5789.60",
    "order_placed_by_name" : "Manish",
    "order_placed_by_no" : "+918878878878",
    "order_quantity" : "80",
    "order_remark" : "1",
    "order_unique_number" : 23,
    "placed_date" : "21-Feb-2020",
    "village_name" : "ehhe"
  } ]
}

FIREBASE FUNCTION LOG:

 sendNotificationToUser
undefined

 sendNotificationToUser
null 

 sendNotificationToUser
Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array. at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:254:16) at Messaging.validateRegistrationTokensType (/srv/node_modules/firebase-admin/lib/messaging/messaging.js:728:19) at Messaging.sendToDevice (/srv/node_modules/firebase-admin/lib/messaging/messaging.js:327:14) at userFCMToken.then.result3 (/srv/index.js:68:34) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)  

Please find out what i have done wrong. Thanks for helping.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *