错误:主题、令牌或条件恰好是必需的

Error: Exactly one of topic, token or condition is required

提问人:TheOne 提问时间:11/17/2023 最后编辑:Frank van PuffelenTheOne 更新时间:11/17/2023 访问量:9

问:

我正在尝试使用 Firebase Cloud Messaging 和云函数为应用实现 PUSH 通知。当用户创建新订单时,会触发 PUSH 通知。

触发按预期发生,但云函数未完成执行。我遇到错误:

只需要一个主题、令牌或条件

我在网上搜索了一下,大多数可能的解决方案都表明,这个错误是通过向Firebase发送一个空的FCM令牌来触发的,但就我而言,我已经验证了FCM令牌确实存在且有效。

我需要帮助,了解此错误的其他可能原因。这是我的云函数代码:

/* eslint-disable linebreak-style */
/* eslint-disable max-len */
/* eslint-disable linebreak-style */
/* eslint-disable indent */
/* eslint-disable no-tabs */
/* eslint-disable linebreak-style */
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

exports.sendOrderNotification = functions.firestore
    .document("pending_orders/{orderId}")
    .onCreate(async (snapshot, context) => {
        const orderData = snapshot.data();
        const dinerFCMToken = orderData.dinerFCMToken;
        console.log("Diner FCM token: ", dinerFCMToken);

        const dinerMessage = {
            notification: {
                title: "New Order Submitted",
                body: "Order submitted successfully. Please await confirmation from the restaurant.",
            },
            token: dinerFCMToken,
        };

        try {
            // Send notification to diner
            await admin.messaging().send(dinerMessage);
            console.log("Notification sent to diner successfully");
        } catch (error) {
            console.log("Error sending notification to diner", error);
        }
    });

下面是一个示例订单对象的样子:

{
    
"dinerPhone":"254722275885", 
"totalBill":"1700.0", 
"orderId":"BUSI4IP9ZU-231117090130.934", 
"coverDetails":"{1=[OrderItem(menuItem=MenuItem(name=Pilau, description=Spicy rice cooked with beef, price=450.0), isSelected=true), OrderItem(menuItem=MenuItem(name=Fruit Juice, description=Fresh fruit juice, price=150.0), isSelected=true)], 2=[OrderItem(menuItem=MenuItem(name=Chicken Tandoori, description=Grilled chicken with tandoori spice and a touch of chilli, price=850.0), isSelected=true), OrderItem(menuItem=MenuItem(name=Brewed Tea, description=Brewed tea, price=250.0), isSelected=true)]}", 
"dinerName":"Kelvin", 
"orderStatus":"PENDING", 
"dinerFCMToken":"cHT1r9MsR_yzGTLzfmEgdX:APA91bEaczwvAJFEYUbu4rSNrQJtQZZQdPxH5_xskfWjFcNKMXb7bDiL3SRw8M9DtOK-A4AmzwtKI47t0nNRCUf1i60mW3sb8ymJ_WVxAn_Ko3grcNmXQsPQqpfDllfccV17KB0LFMv1", 
"restaurantName":"Blue Lagoon Hotel", 
"bookedDate":"18-Nov-2023 13:00", 
"groupSize":2, 
"restaurantFCMToken":"f8Lh-CdrZAzeDWD4K:APA91bEfPxEXG1Rx1kOyZJN77PD9QiH792KxLKMruozVmsGHENXu8EpolWJUSolOExmkujF12UWRATi4WZjYQL8vbwfIJ-zCfIJX96RCOOP9uXdTCPlW5ToNH7ODVBBNONuA06Io2KAy", 
"orderDate":"17-Nov-2023 09:01"
}

我尝试生成一个新的 FCM 令牌,以防万一我拥有的令牌无效。我创建了一个新的,但问题仍然存在。我还尝试在Firebase控制台上进行测试,以查看问题是否出在订单详细信息的传输上,因此我基本上将订单正文粘贴到控制台,但这也导致了内部服务器错误,提示:

类型错误:无法读取 undefined 的属性(读取“fcmToken”)

javascript firebase-cloud-messaging firebase-admin

评论


答: 暂无答案