[英]How to push notifications in firebase node.js cloud functions?
我想用 node.js 创建一个函数,但我遇到了问题。
解释我想做什么:
首先,当一个新文档添加到路径 profile/{profileID}/posts/{newDocument} 时,该函数会触发
该函数将向以下所有用户发送通知。 问题来了。
我在个人资料集合中有另一个集合,它是关注者并包含字段 followerID 的文档。
我想使用这个 followerID 并将其用作文档 ID 来访问我已添加到配置文件文档中的 tokenID 字段。
像这样:
..(个人资料/粉丝ID).get(); 然后访问 tokenID 字段的字段值。
我当前的代码:- Index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.fcmTester = functions.firestore.document('profile/{profileID}/posts/{postID}').onCreate((snapshot, context) => {
const notificationMessageData = snapshot.data();
var x = firestore.doc('profile/{profileID}/followers/');
var follower;
x.get().then(snapshot => {
follower = snapshot.followerID;
});
return admin.firestore().collection('profile').get()
.then(snapshot => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
throw new Error('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": notificationMessageData.title,
"body": notificationMessageData.title,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.title,
"message": notificationMessageData.title
}
}
return admin.messaging().sendToDevice(tokens, payload)
}
})
.catch((err) => {
console.log(err);
return null;
})
});
我的 Firestore 数据库说明。
简介 | 个人资料文档 | 帖子和关注者 | 追随者收集文件和帖子收集文件
我有一个名为 profile 的父集合,它包含文档,因为这些文档包含一个名为 tokenID 的字段并且我想访问它,但我不会仅针对关注者(关注该个人资料的用户)对所有用户执行此操作,因此我'已经创建了一个名为 follower 的新集合,它包含所有关注者 ID,我想获取每个 followerID 并为每个 id 将 tokenID 推送到令牌列表。
如果我正确理解你的问题,你应该做如下。 请参阅以下说明。
exports.fcmTester = functions.firestore.document('profile/{profileID}/posts/{postID}').onCreate((snapshot, context) => {
const notificationMessageData = snapshot.data();
const profileID = context.params.profileID;
// var x = firestore.doc('profile/{profileID}/followers/'); //This does not point to a document since your path is composed of 3 elements
var followersCollecRef = admin.firestore().collection('profile/' + profileID + '/followers/');
//You could also use Template literals `profile/${profileID}/followers/`
return followersCollecRef.get()
.then(querySnapshot => {
var tokens = [];
querySnapshot.forEach(doc => {
// doc.data() is never undefined for query doc snapshots
tokens.push(doc.data().tokenID);
});
var payload = {
"notification": {
"title": notificationMessageData.title,
"body": notificationMessageData.title,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.title,
"message": notificationMessageData.title
}
}
return admin.messaging().sendToDevice(tokens, payload)
});
首先通过做var x = firestore.doc('profile/{profileID}/followers/');
您没有声明DocumentReference
因为您的路径由 3 个元素组成(即 Collection/Doc/Collection)。 另请注意,在 Cloud Functions 中,您需要使用Admin SDK来读取其他 Firestore 文档/集合:因此您需要执行admin.firestore()
( var x = firestore.doc(...)
工作)。
其次,你不能仅仅通过执行{profileID}
来获取profileID
的值:你需要使用context
对象,如下const profileID = context.params.profileID;
.
因此,应用上述内容,我们声明一个CollectionReference
followersCollecRef
并调用get()
方法。 然后我们使用querySnapshot.forEach()
此集合的所有文档以填充tokens
数组。
剩下的部分很简单,并且符合您的代码。
最后,请注意,从 v1.0 开始,您应该使用admin.initializeApp();
简单地初始化您的 Cloud Functions admin.initializeApp();
,请参阅https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-admin
以下 Cloud Function 代码将查找每个关注者的 Profile 文档并使用该文档中tokenID
字段的值。
(请注意,您也可以将tokenID
直接存储在 Follower 文档中。您可能会复制数据,但这在 NoSQL 世界中很常见。)
exports.fcmTester = functions.firestore.document('profile/{profileID}/posts/{postID}').onCreate((snapshot, context) => {
const notificationMessageData = snapshot.data();
const profileID = context.params.profileID;
// var x = firestore.doc('profile/{profileID}/followers/'); //This does not point to a document but to a collectrion since your path is composed of 3 elements
const followersCollecRef = admin.firestore().collection('profile/' + profileID + '/followers/');
//You could also use Template literals `profile/${profileID}/followers/`
return followersCollecRef.get()
.then(querySnapshot => {
//For each Follower document we need to query it's corresponding Profile document. We will use Promise.all()
const promises = [];
querySnapshot.forEach(doc => {
const followerDocID = doc.id;
promises.push(admin.firestore().doc(`profile/${followerDocID}`).get()); //We use the id property of the DocumentSnapshot to build a DocumentReference and we call get() on it.
});
return Promise.all(promises);
})
.then(results => {
//results is an array of DocumentSnapshots
//We will iterate over this array to get the values of tokenID
const tokens = [];
results.forEach(doc => {
if (doc.exists) {
tokens.push(doc.data().tokenID);
} else {
//It's up to you to decide what you want to to do in case a Follower doc doesn't have a corresponding Profile doc
//Ignore it or throw an error
}
});
const payload = {
"notification": {
"title": notificationMessageData.title,
"body": notificationMessageData.title,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.title,
"message": notificationMessageData.title
}
}
return admin.messaging().sendToDevice(tokens, payload)
})
.catch((err) => {
console.log(err);
return null;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.