FCM

Today we will be looking at sending notifications on registered Firebase devices with FCM firebase messaging service by using C#, either it is ASP or Windows Forms app (or simply through CLI) we are going to send notifications to a device which is registered on your firebase app.

Will not be talking about on how to create an app on firebase but will be directly jump on the code. The code is simple.

  
//declare your tagline and message
string tagLine = “The tagline for your notification”;
string message = “The message for your notification”;
try
  {
    string RegisteredIds = "the_registered_device_key";
    var applicationID = "your_application_key";
    var senderId = "your_sender_key";
    WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
    tRequest.Method = "post";
    tRequest.ContentType = "application/json";
    var data = new
                {
                    to = RegisteredIds,
                    content_available = true,

                    notification = new
                    {
                        title = tagLine,
                        body = message,
                        sound = "default",
                        vibrate = true,
                        group = "GROUP",
                        show_in_foreground = true,
                        priority = "high",
                        channel = "Miscellaneous"
                    },
                    priority = "high",
                };
    var serializer = new JavaScriptSerializer();
    var json = serializer.Serialize(data);
    Byte[] byteArray = Encoding.UTF8.GetBytes(json);
    tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
    tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
    tRequest.ContentLength = byteArray.Length;
    using (Stream dataStream = tRequest.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
        using (WebResponse tResponse = tRequest.GetResponse())
        {
           using (Stream dataStreamResponse = tResponse.GetResponseStream())
           using (StreamReader tReader = new StreamReader(dataStreamResponse))
           {
               String sResponseFromServer = tReader.ReadToEnd();
               //Response.Write(sResponseFromServer);
               MessageBox.Show("Notification sent");
           }
        }
     }
}
catch (Exception ex)
{
   //Response.Write(ex.Message);
}

and that’s it, this is the code you can write it behind the onclick event whether it is ASP or anywhere else in the .NET environment.

This method gives us a response (eg. Success, failure or anything), but if you want a method that does not return any response then there is an option for this. Use CorePush nuget package for this purpose. This package helps you send notifications to registered devices (android or iOS). The code for it is as follows:

var fcm = new FcmSender(“your serverKey”, “your senderId”);
await fcm.SendAsync(fcmToken,
new
{
  notification = new
  {
   title = strTitle,
   body = strBody
  },
});

and with no response, this is the code to send notifications.

So, with these approaches, one can send notifications from C#. Happy Coding!