我正在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 enter image description here

推荐答案

您将onCreate()放在另一个函数中,因此与friendRequest不同,它一开始不会部署到云函数中.您似乎正在try 通知已收到请求的用户.您可以try 以下功能:

export const notifyUser = firestore
  .document(`users/{userId}/recievedRequests/{request}`)
  .onCreate(async (snapshot, context) => {
    const userId = context.params.userId;
    const senderName = snapshot.data().name;

    logger.log("New Notification to " + userId + " ,by :" + senderName);

    const payload = {
      notification: {
        title: "New Friend Request",
        body: `Friend Request from ${senderName}`,
      },
    };

    // Get Token of the User
    const tokenSnap = await db
      .collection("users")
      .doc(userId)
      .collection("tokens")
      .doc("token")
      .get();

    const token = tokenSnap.data()!.token;

    return fcm.sendToDevice(token, payload).then((response) => {
      logger.log("Response" + response.successCount);
      return null;
    });
  });

要首先发送请求,只需添加users/RECEIVER_ID/friendRequests/个子集合中的a文档,它将触发上面的函数,该函数将获取接收者的FCM令牌并发送通知.不需要onCall()函数.

Node.js相关问答推荐

使用NodeJS输出检索Base64图像的网络报废

Stripe webhook无法访问Express请求原始正文

使用xml-crypto时出现NodeJS XPath解析错误

GitLab SAST中的Nodejcan未找到匹配项

无法使用Sequelize连接AWS RDS

try 插入重复的邮箱时,mongoDB DuplicateKey 错误未定义

为什么即使数据库确实更新了,此 POST 也无法解析并返回 200 代码?

在nodejs中为oauth请求创建和存储csrf令牌的最佳方法

使用单个 MongoDB 查询更新多个元素

发布请求不使用 Nodejs 更新 MongoDB 中的标头信息

如何使用包含条件正确分页的 sequelize 查询?

如何在带有 JS 的 Nodejs 中使用没有 Async 方法的 Await

Pug - 包括带有include关键字的c代码

什么是nestjs错误处理方式(业务逻辑错误vs.http错误)?

node.js 是否支持yields ?

create-react-app,安装错误(找不到命令)

npm 出现无法读取依赖项错误

npm WARN 不推荐使用 graceful-fs@3.0.8:graceful-fs 版本 3

将 NodeJS 用于大型项目

webpack-dev-server 找不到模块webpack