const admin = require("firebase-admin");
const functions = require("firebase-functions");
admin.initializeApp(functions.config().firebase);
exports.newIssueNotification = functions.firestore.document("main/issues/list/{id}").onCreate((newDocSnap, context) => {
const payload = {
notification: {
title: "New Issue added",
body: "New issue",
//badge: '1',
sound: "default"
}
};
const newDoc = newDocSnap.data();
return newDoc.showGroups.forEach((groupName, context) => {
if(groupName != ""){
return admin.firestore().collection('main').doc('groups').get().then((groupDocSnap, context) => {
const groupDoc = groupDocSnap.data();
return groupDoc.Admin.forEach(uid => {
const docname = uid;
return admin.firestore().collection('users').doc(docname).get().then(uidDocSnap => {
const token = uidDocSnap.data().FCMToken;
return admin.messaging().sendToDevice(token, payload).then(function(response) {
console.log("Notification sent successfully:", response);
})
.catch(function(error) {
console.log("Notification sent failed:", error);
});
})
})
}
)
}else{
}
})
});
对此我是陌生的,任何人都可以帮忙解决一下,以避免此处显示的错误。 console logs
步骤。
- 关于创建新文档
- 在文档中获取组名
- 将文档分组,并获取该组中用户的用户ID
- 使用用户ID获取分配给他们的FCM令牌
- 使用令牌发送通知。