当一个新项目从他订阅的其他用户上传时,我想给一个用户发送消息。
我的方法来处理这个问题:
首先,我检查一个新的项目是否上传。如果这是真的,我希望从上传该项目的用户那里获得uid。然后我转到用户的文件( User /{uid,来自上传项目}的用户),其中订阅他的帐户在一个String列表中,其中包含他们的uid。对于此列表中的每个uid,我希望从具有此特定uid ( user /{uid }/devicetoken)的用户处获取devicetoken,并将每个devicetoken添加到字符串列表中。有了这个设备令牌,我最终想要向列表中的每个设备发送一条消息。但我对js和后端开发没有那么丰富的经验。
我的代码是按照描述的方式进行的,但是当我尝试将它部署到firebase函数时,我会得到一些警告。
部署到防火墙时的警告:
24:17警告避免嵌套承诺/禁止嵌套24:17警告避免嵌套承诺/不嵌套30:26警告不要在循环内设置函数46:20警告避免嵌套承诺46:20避免嵌套承诺/不嵌套承诺
你们中有人能帮我解决我的问题吗?)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initalizeApp(functions.config().firebase);
var msgData;
//listen for new uploades
exports.recipeTrigger = functions.firestore.document('item/{itemId}').onCreate(async(snapshot) => {
msgData = snapshot.data();
//get dat from the user that uploaded the item
await admin.firestore().collection('User/' + msgData.uid).get().then((snapshots) => {
var users = [];
var tokens = [];
if (snapshot.empty) {
console.log('No Users');
return null;
} else {
//get every user uid
users = snapshots.data().subscribers;
for (user of users) {
//user should be the uid
admin.firestore().collection('User/' + user).get().then((snap) => {
for (var token of snap.docs) {
//add devicetoken to a list Avoid nesting promises
tokens.add(token.data().token);
}
return tokens;
}).catch(error => { print(error) })
return users;
}
//specify message content
var payload = {
"notification": {
"title": "From" + snapshots.data().displayName,
"body": "I uploaded a new Recipe",
"sound": "default",
},
"data": {
"senderName": snapshots.data().displayName,
"message": snapshots.data().displayName + "uploaded a new Recipe",
}
}
//send message to devicetokens
return admin.messaging().sendToDevice(tokens, payload).then((response) => {
console.log('Notifications send');
return response;
}).catch((er) => {
console.log(er);
});
}
}).catch(error => { print(error) });
})
发布于 2020-10-20 20:28:03
错误消息告诉您使用firestore令牌查看第23行:
await firestore().document('User/' + user).get().then((snap) => {你可能是想说admin.firestore().doc(...)。
https://stackoverflow.com/questions/64448594
复制相似问题