FCM的Firebase功能不起作用。提供给sendToDevice()的注册令牌必须是非空字符串或非空数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FCM的Firebase功能不起作用。提供给sendToDevice()的注册令牌必须是非空字符串或非空数组相关的知识,希望对你有一定的参考价值。

我有两种情况希望将通知发送到:

1]管理员:下订单时;2]用户:订单状态更新;

我对这两种操作都有两个单独的功能。

用于管理员的FCM云功能可以完美运行。但是用户的FCM功能没有。即使函数的编写方式相同,只是数据库路径不同。

管理员功能

'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');
        );
    );
);

用户功能

'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');
        );
    );
);

数据库结构:


  "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功能日志:

 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)  

请找出我做错了什么。感谢您的帮助。

答案

$ cusNo应该不是const userFCMToken行中的$ userNo吗?

以上是关于FCM的Firebase功能不起作用。提供给sendToDevice()的注册令牌必须是非空字符串或非空数组的主要内容,如果未能解决你的问题,请参考以下文章

FCM 生产 APN 不起作用

Xcode 8.3 Swift 3 FCM 通知上的 Firebase 问题 iOS 10.3 不起作用

Firebase 云消息传递 (FCM) 通知在代理后面的设备上不起作用。有没有其他方法可以解决它?

Firebase 消息传递方法 swizzling 不起作用

更改包名称后 FCM 通知不起作用

Flutter FCM 推送通知不起作用