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