ฟังก์ชันคลาวด์ Firebase ทริกเกอร์ FCM แต่คำขอขาดข้อมูลรับรองการตรวจสอบสิทธิ์ที่จำเป็น

ฉันกำลังพยายามใช้ firebase-admin ภายในฟังก์ชันคลาวด์ของ firebase เพื่อส่งข้อความผ่านการส่งข้อความบนคลาวด์ของ firebase (FCM)

เมื่ออ่านเอกสารประกอบผ่านข้อความดังกล่าว

หากต้องการใช้ Admin FCM API คุณต้องทำตามขั้นตอนในเพิ่ม Firebase Admin SDK ไปยังเซิร์ฟเวอร์ของคุณก่อน

แต่ฉันคิดว่านี่ไม่จำเป็นเพราะฉันแค่ใช้ฟังก์ชั่นคลาวด์เท่านั้น

อย่างไรก็ตาม ทุกอย่างใช้งานได้จนถึงจุดที่ 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 ภายในฟังก์ชันคลาวด์นี้เช่นกัน รูปแบบของโทเค็นอุปกรณ์ถูกต้อง ฉันได้แทนที่ด้วยตัวยึดตำแหน่งสำหรับตัวอย่างนี้

ฉันยังมองหาคำถามที่คล้ายกันไปรอบๆ ด้วย แต่คำถามเดียวที่ฉันพบคือ อันนี้


comment
ฉันคิดว่าข้อความแสดงข้อผิดพลาดที่ส่งโดย FCM ค่อนข้างทำให้เข้าใจผิดที่นี่ ฉันได้รายงานเรื่องนี้ไปยังทีม FCM เพื่อติดตามและปรับปรุงแล้ว   -  person Hiranya Jayathilaka    schedule 14.01.2019


คำตอบ (1)


หลังจากค้นหามามาก ฉันพบรายการข้อผิดพลาดโดย firebase และในรายการนี้ ฉันติดตามรหัสข้อผิดพลาด 'messaging/invalid-apns-credentials' ซึ่งแจ้งว่า:

ไม่สามารถส่งข้อความที่กำหนดเป้าหมายไปที่อุปกรณ์ iOS ได้ เนื่องจากไม่ได้อัปโหลดใบรับรอง APN SSL ที่จำเป็นหรือหมดอายุแล้ว ตรวจสอบความถูกต้องของใบรับรองการพัฒนาและการผลิตของคุณ

ดังนั้นฉันจึงคิดว่าฉันยังไม่ได้ตั้งค่าใบรับรอง "การผลิต" ของฉันและบางที Cordova dev build ของฉันอาจได้รับการปฏิบัติเหมือนเป็น build ที่ใช้งานจริง! ดังนั้นฉันจึงดำเนินการต่อและเพิ่มใบรับรองการผลิต ลบและเพิ่มใบรับรอง dev อีกครั้งเพื่อให้แน่ใจ และมันก็ใช้งานได้

person mesqueeb    schedule 12.01.2019