Here's a complete post on using Firebase Push Notifications in C#, including an explanation of key concepts and a sample implementation using Firebase Cloud Messaging (FCM) with a C# backend (e.g., .NET Core or .NET Framework).
Integrating Firebase Push Notification In C#
Getting Started
Push notifications are a powerful way to engage users and bring them back to your app. Firebase Cloud Messaging (FCM) provides a reliable and efficient connection between your server and devices. Here well learn Integrating Firebase Push Notifications in C#, including how to send messages from a C# server to Android or iOS clients using FCM.
What is Firebase Cloud Messaging (FCM)?
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you send notifications and messages reliably at no cost.
You can send:- Notification messages (display messages in the system tray)
- Data messages (handle custom data in the app)
- Or both at the same time.
Sending Push Notification Using FCM in C#
In this example, we'll create a simple C# console app or service that sends a push notification using FCM HTTP v1 or legacy API. This logic can be used in any C# and .Net applications
Prerequisites- Firebase Project: Create a Firebase project from Firebase Console
- Server Key: Get your Server Key from Project Settings → Cloud Messaging.
- Device Token: Your mobile app must be registered with Firebase and capable of receiving push notifications. You will get a device token from the app, which the server uses to send messages.
Implement Firebase Notification API (Legacy FCM HTTP API for Testing)
- Create a C# Console App
- Install
Newtonsoft.Jsonvia NuGet: if you want learn how to install NuGet package, visit my site "Installing NuGet Packages: A Beginner's Guide". - Use below code in your console application
using System; using System.Net.Http; using System.Text; using Newtonsoft.Json; class Program { static async System.Threading.Tasks.Task Main(string[] args) { var serverKey = "YOUR_FIREBASE_SERVER_KEY"; var senderId = "YOUR_SENDER_ID"; var deviceToken = "TARGET_DEVICE_TOKEN"; var message = new { to = deviceToken, notification = new { title = "Hello from C#", body = "This is a push notification sent using Firebase!", sound = "default" }, data = new { key1 = "value1", key2 = "value2" } }; var jsonMessage = JsonConvert.SerializeObject(message); using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=" + serverKey); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Sender", "id=" + senderId); var content = new StringContent(jsonMessage, Encoding.UTF8, "application/json"); var result = await httpClient.PostAsync("https://fcm.googleapis.com/fcm/send", content); var response = await result.Content.ReadAsStringAsync(); Console.WriteLine("Response: " + response); } } } - Keep your
server keysafe and never expose it in frontend or public repositories.
Migrating to Firebase Notification API (FCM HTTP v1)
Firebase now recommends using the HTTP v1 API which uses OAuth2 and service accounts for authentication.
Steps:- Create a Service Account in Firebase Console → Project Settings → Service Accounts.
- Download the JSON key file.
- Use Google’s OAuth2 token generation or a library like Google.Apis.Auth to authenticate.
Summary
Firebase Push Notification in C# can be implemented quickly using the legacy API for testing and HTTP v1 for production. By leveraging FCM, you can easily send targeted messages, announcements, or alerts to user devices from your C# backend.
Thanks