我正在try 获取用户的用户ID,然后在Firebase函数中的FireStore上运行onCreate函数以获得后台通知,但onCreate函数不运行.函数显示其已执行和已完成.
import { https, firestore, logger } from "firebase-functions";
import { initializeApp, messaging, firestore as _firestore } from "firebase-admin";
initializeApp();
const fcm = messaging();
const db = _firestore();
export const friendRequest = https.onCall((data, context) => {
const userID = context.auth.uid;
try {
db.collection("users")
.doc(userID)
.collection("tokens")
.doc("token")
.get()
.then((value) => {
const token = value.data().token;
firestore
.document(`users/${userID}/recievedRequests/{request}`)
.onCreate((snapshot) => {
const senderName = snapshot.data().name;
logger.log(
"New Notification to " + token + " ,by :" + senderName
);
const payload = {
notification: {
title: "New Friend Request",
body: `Friend Request from ${senderName}`,
},
};
fcm.sendToDevice(token, payload).then((response) => {
logger.log("Response" + response.successCount);
});
});
});
} catch (error) {
logger.log("Error : " + error);
}
});
This is the Friend Request function I want to send notification to user when he receives a notification. My firebase log shows