试图对FCM服务器进行身份验证时,发生了错误'在Firebase云功能上

发布于 2025-02-04 09:55:27 字数 1825 浏览 5 评论 0原文

我试图在Firbase Cloud函数中使用FCM将消息发送给主题时,当创建Firestore文档(消息)时触发。订阅该主题(也使用函数完成)并触发发送函数效果很好,但是实际发送失败的方法是:

Error: 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.

和一些RAW HTML,其中包含< h1> project_not_permitten</h1/h1 < h1> project_not_permittit</h1>

这是我的代码(index.ts):

import * as admin from 'firebase-admin';

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
});

export * from './messages';

and(messages.ts):

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';

export const publishMessage = functions
  .firestore.document('/messages/{messageId}').onCreate(
    (snapshot, context) => {
      const data = snapshot.data();
      const message = {
        notification: {
          title: `${data.sentBy} sent a message`,
          body: data.message,
        },
      };

      return admin.messaging().sendToTopic('messages', message);
    },
  );

根据 https://firebase.google.com/docs/cloud-messaging/auth-server#provide-credentials-using-using-adc 这应该有效。我也尝试过没有任何参数( https://firebase.google .com/docs/admin/setup#initialize-without-without-parameters ),但失败了。我想念什么?

I'm trying to send a message to a topic with FCM in a Firebase Cloud Function triggered when a Firestore document (a message) is created. Subscribing to the topic (also done with Functions) and triggering the send function works fine, but the actual send fails with:

Error: 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.

and some raw HTML containing <H1>PROJECT_NOT_PERMITTED</H1> and <H1>PROJECT_NOT_PERMITTED</H1>.

Here is my code (index.ts):

import * as admin from 'firebase-admin';

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
});

export * from './messages';

and (messages.ts):

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';

export const publishMessage = functions
  .firestore.document('/messages/{messageId}').onCreate(
    (snapshot, context) => {
      const data = snapshot.data();
      const message = {
        notification: {
          title: `${data.sentBy} sent a message`,
          body: data.message,
        },
      };

      return admin.messaging().sendToTopic('messages', message);
    },
  );

According to https://firebase.google.com/docs/cloud-messaging/auth-server#provide-credentials-using-adc this should work. I have also tried doing it without any parameters (https://firebase.google.com/docs/admin/setup#initialize-without-parameters) but it fails all the same. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

相守太难 2025-02-11 09:55:28

事实证明,

      const message = {
        notification: {
          title: `${data.sentBy} sent a message`,
          body: data.message,
        },
      };

      return admin.messaging().sendToTopic('messages', message);

我不需要这个:

      const message = {
        notification: {
          title: `${data.sentBy} sent a message`,
          body: data.message,
        },
        topic: 'messages',
      };

      return admin.messaging().send('messages', message);

Turns out that instead of this

      const message = {
        notification: {
          title: `${data.sentBy} sent a message`,
          body: data.message,
        },
      };

      return admin.messaging().sendToTopic('messages', message);

I needed this:

      const message = {
        notification: {
          title: `${data.sentBy} sent a message`,
          body: data.message,
        },
        topic: 'messages',
      };

      return admin.messaging().send('messages', message);
那支青花 2025-02-11 09:55:28

您可以在项目设置中启用云消息传递API(遗产)/云消息

https://i.sstatic。 net/elwpb.png

you can do enable Cloud Messaging API (Legacy) in Project settings/Cloud Messaging

https://i.sstatic.net/ElwpB.png

最后的乘客 2025-02-11 09:55:28

这是因为Google Firebase版本更改了,admin.messaging()。sendTodeVice()不再盒子。

现在以下内容效果很好:

var admin = require("firebase-admin");
var serviceAccount = require('./xxx.json');

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

const registrationToken = 'fKuD...';

const message = {
  token: registrationToken,
  notification: {
    title: "FCM IS COOL !",
    body: "Notification has been recieved",
    imageUrl: "https://i.ytimg.com/vi/iosNuIdQoy8/maxresdefault.jpg"
  }
}

admin.messaging().send(message);

Note Databaseurl不再需要Firebase-Admin中的数据库。只需在Firebase控制台中下载的私钥即可。

This is because Google Firebase versions changed and admin.messaging().sendToDevice() no longer works out of the box.

Now following works well:

var admin = require("firebase-admin");
var serviceAccount = require('./xxx.json');

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

const registrationToken = 'fKuD...';

const message = {
  token: registrationToken,
  notification: {
    title: "FCM IS COOL !",
    body: "Notification has been recieved",
    imageUrl: "https://i.ytimg.com/vi/iosNuIdQoy8/maxresdefault.jpg"
  }
}

admin.messaging().send(message);

Note databaseURL in firebase-admin is no longer needed. Just private key you download in Firebase Console.

如果没有 2025-02-11 09:55:28

就我而言,使API解决了问题。默认似乎仅
Firebase Cloud Messaging API已启用,但cloud Messaging未启用API。您需要从 apis page 中启用该问题,它将解决该问题

In my case, enabling API solved the issue. Seems like by default only
Firebase Cloud Messaging API is enabled but Cloud Messaging API is not enabled. You need to enable that one from APIs page and it will solve the issue

作业与我同在 2025-02-11 09:55:28

角色/firebase.admin )之后

在我的&lt; projectId&gt;@appspot.gserviceaccount.com中删除( //i.sstatic.net/egyoe.png“ rel =“ nofollow noreferrer”>

由于&lt; projectId&gt;@appspot.gserviceaccount.com服务帐户正在使用Firebase Cloud功能。添加到此服务帐户的角色和权限将其延续到云功能运行时。

如您已经说过的那样,您具有&lt; projectId&gtspot.gserviceaccount.com,请确保它具有(角色/firebase.admin),并且对此文档,它具有cloudMessaging。*权限。

在重新添加&lt; projectId&gt;@appspot.gserviceaccount.com和conerspoding(roles/firebase.admin):

i.sstatic.net/pshkx.png“ rel =“ nofollow 've尝试添加另一个firestore文档来测试该功能(10分钟后),以确保我的权限的更改生效:

“在此处输入图像说明”

对于其他选项,您也可以尝试添加:

  1. firebase成长管理角色/firebase.growthadmin);
  2. 或者,您可以尝试 Firebase Cloud Messaging Api Admin 的Beta(角色/FireBasecloudMessaging.admin),因为它也具有cloudMessaging。*允许。

您还可以检查其他角色具有相应的权限。

After removing the (roles/firebase.admin) in my <projectId>@appspot.gserviceaccount.com, I also encountered your error:

enter image description here

Since the <projectId>@appspot.gserviceaccount.com service account is being used by Firebase Cloud Functions. Roles and permissions added to this service account carry over to the Cloud Functions runtime.

As you've already stated, you have <projectId>@appspot.gserviceaccount.com, make sure that it has a (roles/firebase.admin), and according to this documentation, it has a full access for cloudmessaging.* permission.

After I re-added the <projectId>@appspot.gserviceaccount.com and the correspoding (roles/firebase.admin):

enter image description here

I've tried adding another Firestore document to test the function (after 10 minutes or so) just to make sure that the changes in my permissions have taken effect:

enter image description here

For other options, you can also try to add the:

  1. Firebase Grow Admin (roles/firebase.growthAdmin);
  2. Or you can try the beta of Firebase Cloud Messaging API Admin (roles/firebasecloudmessaging.admin) since it also has cloudmessaging.* permission.

You can also check the other roles with it's corresponding permissions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文