从云函数发送 Firebase 云消息时获取访问令牌时出错

问题描述 投票:0回答:1

我尝试使用云函数通过下面的代码向 iOS 设备发送消息,但是当我使用以下代码将其部署在终端中时,它会生成以下错误:

firebase deploy --only functions

发送消息时出错:FirebaseAppError:通过“credential”属性向initializeApp()提供的凭证实现无法获取有效的Google OAuth2访问令牌,并出现以下错误:“获取访问令牌时出错:发出请求时出错:getaddrinfo ENOTFOUNDmetadata.google .内部。错误代码:ENOTFOUND”。

import * as admin from "firebase-admin"; import * as serviceAccount from "../../credentials/my-app-credentials.json"; admin.initializeApp({ // credential: admin.credential.cert(serviceAccount), credential: admin.credential.cert(serviceAccount as admin.ServiceAccount), }); const message: admin.messaging.Message = { notification: { title: "Test notification", body: "Sent from firebase cloud functions", }, token: "<REDACTED>", }; admin.messaging().send(message) .then((response) => { console.log("Successfully sent message:", response); }) .catch((error) => { console.error("Error sending message:", error); });
其他注意事项:

    我正在进行火焰计划
  • 当在 firebase 中使用带有测试消息的撰写通知页面时,它可以正常工作,并且会被传递到 iOS 设备。
  • 我已经设置了一个带有密钥的服务帐户
  • 访问我的 Firebase 数据库工作的其他功能
  • 我尝试了一些变体来执行 admin.initializeApp,但没有一个起作用
  • 我是使用云函数的新手
node.js google-cloud-functions firebase-cloud-messaging firebase-admin
1个回答
0
投票
确保

my-app-credentials.json

 与您的 Cloud Functions 源文件位于同一目录中,或在该目录下。如果不是,则不会部署它,因此当该函数在 Google Cloud Platform 上运行时不会出现。

© www.soinside.com 2019 - 2024. All rights reserved.