Firebase消息:如何配置无服务器功能



我正在尝试设置一个vercel函数来处理(un)订阅firebase消息传递。关于服务器端的文档很少,所以我很纠结。

我相信我有正确的服务工作者帐户json在admin.json,但这段代码是失败的错误FirebaseError: Firebase: Need to provide options, when not being deployed to hosting via source.

import admin from 'firebase-admin';
import { initializeApp } from 'firebase-admin/app';
import serviceAccount from './admin.json' assert { type: 'json' };
import { getMessaging } from 'firebase/messaging';
export default function messaging(request, response) {
// Q1 should this be inside the function or outside?
const app = initializeApp({
credential: admin.credential.cert(serviceAccount)
});
// body is a string
let body = JSON.parse(request.body);
if (body.action === 'subscribe') {
// this is where the error is coming in
return getMessaging()
.subscribeToTopic(body.token, 'mol23-general')
.then((res) => {
response.status(200).json({ result: res });
})
.catch((error) => {
console.log('Error subscribing to topic:', error);
response.status(500).json({ error });
});

使用了错误版本的getMessaging

import { getMessaging } from 'firebase-admin/messaging';

最新更新