How to send push notification from .net6 to firebase, could you please explain with code?
Loading
How to send push notification from .net6 to firebase, could you please explain with code?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pasang TamangRank #449 · 5.6K reputationPosted Apr 3, 2023, 12:50 AM EDT
Hi,
Please check one of the old article from C# Corner https://www.c-sharpcorner.com/blogs/how-to-send-firebase-notification-from-c-sharp. You can find code example with detail explanation.
Regards,
Pasang
Amit MohantyRank #112 · 20K reputationPosted Apr 3, 2023, 12:11 AM EDT
Check the below example code that shows how to send push notifications from a .NET 6 application to Firebase Cloud Messaging (FCM) using the Firebase Admin SDK:
using FirebaseAdmin; using FirebaseAdmin.Messaging; using Google.Apis.Auth.OAuth2; using System.Threading.Tasks; var credential = GoogleCredential.FromFile("path/to/credentials.json"); var firebaseApp = FirebaseApp.Create(new AppOptions() { Credential = credential, }); var messaging = FirebaseMessaging.GetMessaging(firebaseApp); var message = new Message() { Notification = new Notification() { Title = "New message", Body = "You have a new message" }, Token = "FCM registration token", }; message.Data["key"] = "value"; var response = await messaging.SendAsync(message);Latha Donepudi30 reputationPosted Apr 3, 2023, 2:19 AM EDT
could you please share me full details by steb by step
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 3, 2023, 2:22 PM EDT
using FirebaseAdmin; using FirebaseAdmin.Messaging; using Google.Apis.Auth.OAuth2; using System; using System.Threading.Tasks; try { var credentials = GoogleCredential.FromFile(Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS")); var app = FirebaseApp.Create(new AppOptions { Credential = credentials, }); var messaging = FirebaseMessaging.DefaultInstance; var registrationToken = "FCM registration token"; // Retrieve the token dynamically var message = new Message { Notification = new Notification { Title = "New message", Body = "You have a new message", }, Token = registrationToken, Data = { ["key"] = "value", }, }; var response = await messaging.SendAsync(message); Console.WriteLine("Message sent successfully: " + response); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); }The code already uses await for the SendAsync method, but it's a good practice to use async and await throughout the code whenever possible.