我正在使用Firebase云函数向我的用户发送特定数据库事件的推送通知,例如在创建数据库条目时。
我遵循以下步骤:
-
创建
onCreate
方法
-
例如,从其他数据库位置获取附加数据以获取用户信息
-
发送推送通知
exports.NewEntryNotification = functions.database.ref("entries/{entry}").onCreate((snapshot, context) => {
let guestbookID = snapshot.child("guestbookID").val();
admin.database().ref("guestbooks").orderByChild("id").equalTo(guestbookID).once("value", (snapshot) => {
snapshot.forEach((child) => {
let guestbookOwnerID = child.child("owner").val();
admin.database().ref("users").orderByChild("uid").equalTo(guestbookOwnerID).once("value", (snapshot2) => {
snapshot2.forEach((child2) => {
// Token is:
let userToken = child2.child("pn_token").val();
// If token exists
if (userToken) {
// Push Notification
const payload: admin.messaging.MessagingPayload = {
notification: {
titleLocKey: "new_entry_title",
bodyLocKey: "new_entry_desc",
badge: "1",
}
}
fcm.sendToDevice(userToken, payload);
}
})
});
})
})
});
问题是:我的函数处理速度非常慢。我等了将近5分钟,直到推送通知发送。我认为我在代码中使用了太多的循环。但我没有找到其他方法来获取数据。
这就是原因吗?或者你在我的代码中发现了其他错误?