我想知道是否有人可以帮助启发我如何成功执行此代码,它等到整个数组充满FCM令牌后再触发sendToDevice()
。
我一直在使用这些链接(下面列出(作为参考来尝试解决此问题,但我仍然无法弄清楚,所以唉,我在这里寻求指导。我只需要在tokens
数组完全填充后传递它。我已经在每次push
上多次触发它的地方得到了它,但从来没有在异步加载然后触发><</p>
用于文件上传的 Firebase 实时数据库结构
Promise.all with Firebase DataSnapshot.forEach
https://aaronczichon.de/2017/03/13/firebase-cloud-functions/
exports.sendVenueAnnouncement = functions.database.ref(`/venueAnnouncements/{venueUid}/announcement`).onCreate(event => {
const venueUid = event.params.venueUid;
const announcement = event.data.val();
const getVenueDisplaynamePromise = admin.database().ref(`verifiedVenues/${venueUid}/displayname`).once('value');
return getVenueDisplaynamePromise.then(snapshot => {
const displayname = snapshot.val();
const payload = {
notification: {
title: `${displayname}`,
body: `${announcement}`
}
};
const getSubscriberTokensPromise = admin.database().ref(`subscribers/${venueUid}`).once('value');
return getSubscriberTokensPromise.then(snapshot => {
const tokens = [];
snapshot.forEach(function(childSnapshot) {
const key = childSnapshot.key;
const token = admin.database().ref(`pushTokens/` + key).once('value');
tokens.push(token);
});
return Promise.all(tokens);
}, function(error) {
console.log(error.toString())
}).then(function(values) {
return admin.messaging().sendToDevice(values, payload).then(response => {
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove)
});
})
})
})
你几乎对承诺有所了解。看起来您还将回调与承诺混合在一起。Firebase 和 Cloud Functions for Firebase 完全基于 Promise,因此没有必要。
话虽如此,您的代码应如下所示:
exports.sendVenueAnnouncement = functions.database
.ref(`/venueAnnouncements/${venueUid}/announcement`)
.onCreate(event => {
const venueUid = event.params.venueUid
const announcement = event.data.val()
let payload
let tokens = []
return admin.database()
.ref(`verifiedVenues/${venueUid}/displayname`)
.once('value')
.then(snapshot => {
const displayname = snapshot.val()
payload = {
notification: {
title: `${displayname}`,
body: `${announcement}`
}
}
return admin.database().ref(`subscribers/${venueUid}`).once('value')
})
.then(snapshot => {
snapshot.forEach((childSnapshot) => {
const key = childSnapshot.key
const token = admin.database().ref(`pushTokens/` + key).once('value')
tokens.push(token)
})
return Promise.all(tokens)
})
.then(values => {
return admin.messaging().sendToDevice(values, payload)
})
.then(response => {
const tokensToRemove = []
response.results.forEach((result, index) => {
const error = result.error
if (error) {
if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove())
}
}
})
return Promise.all(tokensToRemove)
})
})
请注意,我没有将 Promise 分配给变量。只需将其退回并链接then
.一个承诺可以返回另一个承诺。
我建议观看这个Firecast,以便更好地理解承诺。