多次调用Firebase云函数
您好,当我没有检查previous.exists()时,会多次调用我的firebase cloud函数。 我收到多个推送通知多次调用Firebase云函数,firebase,firebase-cloud-messaging,google-cloud-functions,Firebase,Firebase Cloud Messaging,Google Cloud Functions,您好,当我没有检查previous.exists()时,会多次调用我的firebase cloud函数。 我收到多个推送通知 if (!event.data.exists()){ return; } if (event.data.previous.exists()){ return; } 但是当我检查它时,我没有收到推送通知。 以下是无效代码: 我应该换什么 exports.sendShoppingListInvitationNotification = functions.d
if (!event.data.exists()){
return;
}
if (event.data.previous.exists()){
return;
}
但是当我检查它时,我没有收到推送通知。
以下是无效代码:
我应该换什么
exports.sendShoppingListInvitationNotification = functions.database.ref('/invites/{id}/').onWrite(event => {
//get the snapshot of the written data
const snapshot = event.data;
if (!event.data.exists()){
return;
}
if (event.data.previous.exists()){
return;
}
//get snapshot values
console.log(snapshot.key);
const receiptToken = snapshot.child('receiptFcmToken').val();
const senderName = snapshot.child('senderNickname').val();
const inviteMessage = snapshot.child('inviteMessage').val();
const senderImage = snapshot.child('senderProfileImageURL').val();
//create Notification
const payload = {
notification: {
title: `Invitation from ${senderName}`,
body: `${inviteMessage}`,
icon: `${senderImage}`,
badge: '1',
sound: 'default',
}
};
//send a notification to firends token
return admin.messaging().sendToDevice(receiptToken, payload).then(response => {
console.log("Successfully sent message:", response);
}).catch((err) => {
console.log(err);
});
});
我在云控制台上没有收到错误消息
这是firebase结构:
似乎不应该多次调用它,除非您对该位置进行多次写入。如果只想在第一次写入路径时发送通知,请尝试使用
.onCreate
而不是.onWrite
。那么您就不需要对以前的数据进行检查。请参阅概述不同数据库触发器的文档。好主意。我刚看了你的教程。很荣幸能得到专家的回答。遗憾的是,我现在没有得到我的数据。onCreate函数为receiptFcmToken传递null。错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组。我想我是在调用onWrite时想到的。添加时,我的函数在每个子节点上执行。onCreate在创建时和添加childs之前提供了一个空节点。但是我如何得到孩子们的价值观。我有点困惑。你是如何将数据添加到数据库的?我测试了你的代码,它按预期工作。但是,如果要一次或一组而不是一个子项添加每个id
的一些子项,则每次添加都将调用该函数。将数据添加到数据库的代码是什么?似乎假设cloud firestore触发的函数只执行一次是不安全的:请参见此处和此处:我遇到了聚合问题。真的认为应该修改文档来强调这一点:如果不是幂等的话,可能很少发生,而且是一个痛苦的bug。