【问题标题】:How can I use FCM from within Firebase function如何在 Firebase 函数中使用 FCM
【发布时间】:2021-01-25 16:45:48
【问题描述】:

我正在尝试从 firebase 函数中发送 FCM,但每次都收到以下错误:

await admin.messaging().sendToDevice(registrationToken, message)

我听从了this post, of the same topic 的建议,没有任何帮助。指向文章中的另一个链接 Add the Firebase Admin SDK to your server 并不是很有帮助,因为我正在使用 Firebase Server。

这篇文章,同样,Sending FCM using Firebase Functions 和我使用相同的代码(FCM 代码),同样的错误。

这是完整的错误:

Error sending message: { 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 server response: "<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
". Status code: 401.
    at FirebaseMessagingError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:43:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:89:28)
    at new FirebaseMessagingError (/workspace/node_modules/firebase-admin/lib/utils/error.js:255:16)
    at Object.createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:57:12)
    at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:78:51
    at process._tickCallback (internal/process/next_tick.js:68:7)
  errorInfo:
   { code: 'messaging/authentication-error',
     message:
      '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 server response: "<HTML>\n<HEAD>\n<TITLE>Unauthorized</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Unauthorized</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n". Status code: 401.' },
  codePrefix: 'messaging' } 

【问题讨论】:

    标签: firebase firebase-cloud-messaging


    【解决方案1】:

    我是如何解决的:

    1. 转到Google Service Account 并选择您的项目。
    2. 它将显示一个帐户列表。点击帐户something@appspot.gserviceaccount.com
    3. 选择选项卡“键”。生成新密钥并将其下载为 JSON。
    4. 最后,使用 .cert() 函数将生成的 JSON 添加到您的项目中:

    这仅用于测试,请记住在部署函数时删除凭据!

    admin.initializeApp({
      credential: admin.credential.cert(
        "route/to/generatedJSON.json"
      ),
    });
    const messaging = admin.messaging();

    【讨论】:

      【解决方案2】:

      好的,不相关但相关的我找到了答案...

      gcp-the-caller-does-not-have-a-permission

      基本上按照these的说明添加

      Firebase 管理员

      到您的 .gserviceaccount.com 帐户。

      【讨论】:

        猜你喜欢
        • 2017-08-03
        • 2018-04-14
        • 1970-01-01
        • 2017-11-02
        • 1970-01-01
        • 2018-06-21
        • 2021-08-13
        • 2022-01-17
        • 1970-01-01
        相关资源
        最近更新 更多