Firebase 云功能触发 FCM 但请求缺少必需的身份验证凭据

Firebase cloud function triggers FCM but Request is missing required authentication credential

我正在尝试在 firebase 云功能中使用 firebase-admin 通过 firebase 云消息传递 (FCM) 发送消息。

阅读documentation时它说

To use the Admin FCM API, you must first follow the steps in Add the Firebase Admin SDK to your Server.

但我认为这不是必需的,因为我只使用云功能?

无论如何,一切正常,直到 admin.messaging().send 出现此错误:

Error sending message: { Error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
    at FirebaseMessagingError.Error (native)
    at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
    at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16)
    at FirebaseMessagingRequestHandler.handleHttpError (/user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:125:50)
    at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:113:23
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
  errorInfo: 
   { code: 'messaging/invalid-apns-credentials',
     message: 'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.' },
  codePrefix: 'messaging' }

这是我的云函数源码

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

admin.initializeApp(functions.config().firebase)
const firestore = admin.firestore()
firestore.settings({timestampsInSnapshots: true})

exports.notification = functions.firestore
  .document(path)
  .onUpdate(async (change, context) => {
    const deviceTokens = ['deviceToken-123123123']
    deviceTokens.forEach(token => {
      const fcmMessage = {
        notification: {title: 'test title', body: 'test body'},
        token
      }
      admin.messaging().send(fcmMessage)
        .then((response) => {
          // Response is a message ID string.
          console.log('Successfully sent message:', response)
        })
        .catch((error) => {
          console.log('Error sending message:', error)
        })
    })
  })

设备令牌存储在 firestore 中,并在此云函数内从 firestore 检索。设备令牌的格式正确。我已将其替换为此示例的占位符。

我也四处寻找类似的问题,但我唯一能找到的是 this one

经过多次搜索,我找到了一个 error list by firebase,在这个列表中,我追踪到错误代码 'messaging/invalid-apns-credentials',它说:

A message targeted to an iOS device could not be sent because the required APNs SSL certificate was not uploaded or has expired. Check the validity of your development and production certificates.

所以我想,我还没有设置我的 "production" 证书,也许我的 cordova 开发构建被视为生产构建!所以我只是继续添加生产证书,还删除并重新添加开发证书以确保成功。