A modern, lightweight, and easy-to-use sample project for implementing Firebase Cloud Messaging (FCM) push notifications. Built with:
- .NET 10 Minimal API for a clean and performant backend.
- React + TypeScript + Vite for a fast, type-safe frontend.
Perfect for developers looking to quickly integrate push notifications into their apps or learn FCM with a production-ready example.
- 🎯 Minimal & Modern: Clean code with .NET Minimal APIs and React + Vite for fast development.
- 🔒 Secure: Follows Firebase best practices for secure FCM integration.
- ⚡ Production-Ready: Includes OpenAPI docs, TypeScript, and environment-based config.
- 📚 Beginner-Friendly: Clear setup guides and well-documented code.
- 🌐 Cross-Platform: Works seamlessly for web notifications in foreground and background.
- Project Structure
- Features
- Backend: fcm-push-api
- Frontend: fcm-react-sample
- How to Use
- Security Notes
- Contributing
- License
📂 fcm-push-api/ # .NET Minimal API for sending FCM notifications
📂 fcm-react-sample/ # React + TypeScript frontend for receiving notifications
- Sleek Backend: .NET 10 Minimal API for simple, high-performance endpoints.
- Firebase Integration: Uses Firebase Admin SDK for secure push notifications.
- Interactive Frontend: React + TypeScript with react-toastify for beautiful notifications.
- Type Safety: TypeScript on both frontend and backend.
- API Docs: Auto-generated OpenAPI/Swagger UI for easy testing.
- Flexible Config: Environment-based setup for secure secrets management.
A lightweight .NET 10 API for sending FCM push notifications to single or multiple devices.
-
Clone the repo and navigate to
fcm-push-api
:cd fcm-push-api
-
Add your Firebase
serviceAccountKey.json
(download from Firebase Console) to thefcm-push-api
folder. -
Update
appsettings.json
with the service account path:"Firebase": { "ServiceAccountPath": "serviceAccountKey.json" }
-
Run the API:
dotnet restore dotnet run
Access the API at
http://localhost:5005
(configurable in Properties/launchSettings.json).
-
POST
/api/notification/send
Send a notification to a single device.{ "token": "FCM_DEVICE_TOKEN", "title": "Hello", "body": "World", "data": { "key": "value" } // optional }
-
POST
/api/notification/send-bulk
Send notifications to multiple devices.{ "tokens": ["TOKEN1", "TOKEN2"], "title": "Bulk Title", "body": "Bulk Body", "data": { "foo": "bar" } // optional }
-
Interactive Docs: Visit
http://localhost:5005/scalar
for Swagger UI in development.
- appsettings.json: Configures logging and Firebase settings.
- Program.cs: Defines Minimal API setup and endpoints.
- Services/FcmService.cs: Handles FCM logic.
- Endpoints/NotificationEndpoints.cs: API endpoint definitions.
A modern React + TypeScript app for receiving and displaying FCM push notifications.
-
Navigate to the frontend folder:
cd fcm-react-sample
-
Install dependencies:
npm install
-
Add your public VAPID key to
.env
:VITE_PUBLIC_VAPID_KEY=YOUR_PUBLIC_VAPID_KEY
(Find this in your Firebase project settings.)
-
Start the dev server:
npm run dev
Access the app at
http://localhost:5173
.
- FCM Setup: Requests notification permission and registers a service worker.
- Token Retrieval: Logs the FCM token to the browser console.
- Foreground Notifications: Displays toast notifications using react-toastify.
- Background Notifications: Uses a service worker for system notifications when the app is closed.
Key Files:
src/services/notificationService.ts
: Manages FCM registration and messages.src/components/notification.tsx
: Renders toast notifications.public/firebase-messaging-sw.js
: Handles background notifications.
- Start the backend (
dotnet run
infcm-push-api
). - Start the frontend (
npm run dev
infcm-react-sample
). - Open the React app and allow notifications.
- Copy the FCM token from the browser console.
- Send a notification via the API (use Swagger UI or an HTTP client like Postman).
- See notifications in the browser (toast for foreground, system for background).
- 🚨 Keep
serviceAccountKey.json
private and never commit it to Git. - ✅ VAPID public key is safe to expose; keep the private key secret.
- 🔐 Production: Use HTTPS and add authentication to your API endpoints.
We'd love your contributions! Here's how to get started:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/awesome-feature
). - Commit your changes (
git commit -m 'Add awesome feature'
). - Push to the branch (
git push origin feature/awesome-feature
). - Open a Pull Request.
Check out our CONTRIBUTING.md for more details.
Licensed under the MIT License. Feel free to use, modify, and share!
Star this repo ⭐ to support the project and start sending push notifications today!