我尝试使用云函数通过下面的代码向 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);
});
其他注意事项:
my-app-credentials.json
与您的 Cloud Functions 源文件位于同一目录中,或在该目录下。如果不是,则不会部署它,因此当该函数在 Google Cloud Platform 上运行时不会出现。