尝试使用Firebase函数从云修复中获取我的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);
});我的萤火虫
帖子:

用户:

如果我手动添加令牌一切正常,但只发送每一个“喜欢”到一个设备,我的目标是发送一个链接,只有所有者的帖子。
发布于 2021-03-17 18:26:45
Martin的答案是正确的,但是似乎ref字段是类型引用的,请参阅开头的斜杠,加上您得到的错误。
因此,如果这个假设是正确的,那么您应该使用path属性,如下所示(修改Martin的代码):
let userRef = event.after.get('ref'); // obviously the path is mandatory ...
let userDoc = await admin.firestore().doc(userRef.path).get(); // then this should match
let token = userDoc.get('token'); // and the token should be accessible此外,为了正确地实现管理云功能的生命周期,您应该在最后做以下工作:
let response = await admin.messaging().send(message);
console.log(response);
return null;或者简单的
return admin.messaging().send(message);发布于 2021-03-17 14:32:37
可能更相似的是:
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)); ...viewable at https://console.cloud.google.com/logs/query。当侦听Posts/{likes}时,您可能需要一个额外的查询--当侦听Posts时,您需要确定更改。访问ref是使后续查询工作所必需的。
https://stackoverflow.com/questions/66675005
复制相似问题