NodeJS 401错误https://fcm.googleapis.com/fcm/send可以使用admin().messaging().send()但不能使用admin().messaing().sendTopic()

inn6fuwd  于 2023-02-03  发布在  Node.js
关注(0)|答案(1)|浏览(129)

我已经在节点js中创建了我的后端,如下所示。当调用代码示例1时,代码执行没有问题。示例1调用admin().messaging().send()并工作;然而,当调用代码示例2(发送主题消息)时,出现401错误(如下所示)

An error occurred when trying to authenticate to the FCM servers. 
Make sure the credential used to authenticate this SDK has the proper permissions. 
See https://firebase.google.com/docs/admin/setup for setup instructions.

PROJECT_NOT_PERMITTED
Error 401

发送主题消息时是否有权限设置?401错误需要怎么解决?谢谢
示例1

// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "ap-northeast-2" });

var admin = require("firebase-admin");

var serviceAccount = require("../firebadeCredentialInformation.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

module.exports.handler = async (event) => {
  console.log(event);
  var body = JSON.parse(event.body);
  console.log(body);
  var topic;
  var topicPayload;

  try {
    //body, title, data, token
    const message = {
      data: {
        type: "VOTE",
        senderName: body.senderName,
        question: body.question,
        questionRangeKey: body.questionRangeKey,
        senderToken: body.senderToken,
        gender: body.gender,
        schoolGrade: body.schoolGrade,
        schoolName: body.schoolName,
      },
      token: body.token,
    };
    //? Make sure the message is sent
    //TODO REMOVE BELOW AWAIT TO IMPROVE SPEED
    var result = await admin.messaging().send(message);
    console.log(result);

    return {
      statusCode: 200,
      body: JSON.stringify(
        {
          message: "Sucessfully sent response",
          input: event,
        },
        null,
        2
      ),
    };
  } catch (e) {
    console.log("Error", e);
    return {
      statusCode: 500,
      body: JSON.stringify(
        {
          message: e,
          input: event,
        },
        null,
        2
      ),
    };
  }
};

示例2

// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "ap-northeast-2" });

var admin = require("firebase-admin");

var serviceAccount = require("../firebadeCredentialInformation.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

module.exports.handler = async (event) => {
  console.log(event);
  var body = JSON.parse(event.body);
  console.log(body);
  var topic;
  var topicPayload;

  try {
    //body, title, data, token
    const message = {
      data: {
        type: "VOTE",
        senderName: body.senderName,
        question: body.question,
        questionRangeKey: body.questionRangeKey,
        senderToken: body.senderToken,
        gender: body.gender,
        schoolGrade: body.schoolGrade,
        schoolName: body.schoolName,
      },
      token: body.token,
    };
    //? Make sure the message is sent
    //TODO REMOVE BELOW AWAIT TO IMPROVE SPEED
    var result = await admin.messaging().send(message);
    console.log(result);

    if (body.schoolId != "") {
      //* Ff not missing school id
      const topicPayload = {
        data: {
          type: "TOPIC",
          senderName: body.senderName,
          question: body.question,
          questionRangeKey: body.questionRangeKey,
          senderToken: body.senderToken,
          gender: body.gender,
          schoolGrade: body.schoolGrade,
          schoolName: body.schoolName,
        },
      };
      const schoolId = body.schoolId;
      const topic = "/topics/" + schoolId;
      console.log(topic);

      //? Make sure the message is sent
      //TODO REMOVE BELOW AWAIT TO IMPROVE SPEED
      // Send a message to devices subscribed to the provided topic.
      var topicResult = await admin
        .messaging()
        .sendToTopic(topic, topicPayload);
      console.log(topicResult);
    }

    return {
      statusCode: 200,
      body: JSON.stringify(
        {
          message: "Sucessfully sent response",
          input: event,
        },
        null,
        2
      ),
    };
  } catch (e) {
    console.log("Error", e);
    return {
      statusCode: 500,
      body: JSON.stringify(
        {
          message: e,
          input: event,
        },
        null,
        2
      ),
    };
  }
};
yks3o0rb

yks3o0rb1#

我已经为那些被卡住的人解决了这个问题。我没有使用admin().message.sendTopic()。我使用了下面的命令,它也与发送主题消息相同,它起作用了。

//* if not missing school id
      const schoolId = body.schoolId;
      const topic = "/topics/" + schoolId;

      const topicPayload = {
        data: {
          type: "TOPIC",
          senderName: body.senderName,
        },
        topic: topic,
      };

      console.log(topic);

      // Send a message to devices subscribed to the provided topic.
      var topicResult = await admin.messaging().send(topicPayload);

相关问题