尝试使用Firebase函数从我的Cloud Firestore中获取我的FCM令牌
我的功能代码:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotificationToFCMToken = functions.firestore.document('Posts/{likes}').onWrite(async (event) => {
const title = event.after.get('title');
const content = event.after.get('likes');
let userDoc = await admin.firestore().doc('Users').get();
let fcmToken = userDoc.get('{token}');
var message = {
notification: {
title: title,
body: "you have a new like",
},
token: fcmToken,
}
let response = await admin.messaging().send(message);
console.log(response);
});
我的Firestore
岗位:
用户:
如果我手动添加令牌,所有东西都可以工作,但只是将每个“喜欢”发送到一个设备,我的目标是只发送一个链接给帖子的所有者
可能更像这样:
let userRef = event.after.get('ref'); // obviously the path is mandatory ...
let userDoc = await admin.firestore().doc(userRef).get(); // then this should match
let token = userDoc.get('token'); // and the token should be accessible
添加日志以查看所获得的内容:functions.logger.info(''+json.stringify(event));
...可在https://console.cloud.google.com/logs/query上查看。在侦听帖子/{likes}
时,您可能需要一个额外的查询,而在侦听帖子
时,您需要确定更改。需要获得对ref
的访问才能使后续查询工作。