Firebase Notification Using C#

In this article, we will explore how to send notifications using C# and Firebase Cloud Messaging (FCM)

What Is Firebase Cloud Messaging (FCM)?

Firebase Cloud Messaging (FCM) is a Cloud solution for messaging or notification for Android, iOS, and Web Applications, that lets you reliably send messages at no cost.

It allows the third-party application developers to send notifications or messages from a server to the user of that application.

Implementation:

  • create the following method to send FCM notification
    public void SendMessage()
          {
              string serverKey = "Your Server Key ";
              var notificationInputDto = new
              {
                  to = "client Device token",
                  notification = new
                  {
                      body = "Body of your Notification",
                      title = "Title of your Notification ",
                      icon = "",
                      type = ""
                  },
                  data = new
                  {
                      key1 = "value1",
                      key2 = "value2"
                  }
              };
              try
              {
                  var result = "";
                  var webAddr = "https://fcm.googleapis.com/fcm/send";
    
                  var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                  httpWebRequest.ContentType = "application/json";
                  httpWebRequest.Headers.Add("Authorization:key=" + serverKey);
                  httpWebRequest.Method = "POST";
    
                  using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                  {
    
                      streamWriter.Write(JsonConvert.SerializeObject(notificationInputDto));
                      streamWriter.Flush();
                  }
    
                  var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                  {
                      result = streamReader.ReadToEnd();
    
                  }
    
    
              }
              catch (Exception ex)
              {
                  throw;
              }
          }
  • In the above code replace Your Server key with your legal server key
Note: You have to copy Legacy Server Key from Firebase Console > Project Settings > Cloud Messaging
var notificationInputDto = new
           {
               to = "client Device token",
               notification = new
               {
                   body = "Body of your Notification",
                   title = "Title of your Notification ",
                   icon = "",
                   type = ""
               },
               data = new
               {
                   key1 = "value1",
                   key2 = "value2"
               }
           };

In the above code of notificationInputDto replace the values of the properties with your values like :

  • to:-pass Your client’s Device Token.
  • title:- pass the title of the notification.
  • body:- pass the body of the notification.
  • Data:- Key-Value pair of Additional data to send in the notification.
  • icon:-pass Icon you want to send for your notifications(optional).

Hope this article helps you guys.

1 Comment

  1. Bobby Dev

    Hi,

    This is not working. No Error and the Success is 1 but still not receiving notification.

    Is the Legacy API not working. It used to work earlier but started failing in the last 2 days.

    Can you please test and let me know if its working for you?

    0
    0
    Reply

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories