繁体   English   中英

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

[英]Firebase messaging : how to configure serverless function

我正在尝试设置一个 vercel 函数来处理(取消)订阅 firebase 消息。 服务器端的文档很少,所以我很挣扎。

我相信我在admin.json上有正确的服务工作人员帐户 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';
 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM