import { VercelRequest, VercelResponse } from '@vercel/node'; import { initializeApp, cert } from 'firebase-admin/app'; import { getFirestore } from 'firebase-admin/firestore'; import { getMessaging } from 'firebase-admin/messaging'; import { Message } from 'firebase-admin/lib/messaging/messaging-api'; const serviceAccountStringified = Buffer.from(process.env.FIREBASE_SERVICE_ACCOUNT!, 'base64').toString('ascii'); const serviceAccount = JSON.parse(serviceAccountStringified); initializeApp({ credential: cert(serviceAccount), }); const firestore = getFirestore(); const messaging = getMessaging(); export default async function sendPushNotification(req: VercelRequest, res: VercelResponse) { const { user_id, title, body } = req.query as { user_id: string; title: string; body: string }; const friend = await firestore .collection('Users') .doc(user_id) .get(); if (!friend.exists) { return res.status(400).json({ error: "User doesn't exist" }); } const token = friend.data()!['token']; const message: Message = { notification: { title, body, }, token, }; messaging .send(message) .then((response) => { console.log('Successfully sent message:', response); res.status(200); }) .catch((error) => { console.log('Error sending message:', error); res.status(500).send(error); }); }