Mohit Solanki Mohit Solanki - 1 month ago 44
C# Question

How to implement FCM in ASP.Net C#?

I am using GCM for notifications on Android with c#, but now I want to change GCM to FCM.

What do I have to change for that? Or how can I implement FCM in C#? Any help is appreciated. I am currently using this code for FCM, I just changed URL but it's not working.

try {
msg = message;

var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var SENDER_ID = "xxxxxxxxxxx";

WebRequest tRequest;
//tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";

tRequest.ContentType = "application/x-www-form-urlencoded";
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));

tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + message + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + notificationtoken + "";

Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;

Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

WebResponse tResponse = tRequest.GetResponse();

dataStream = tResponse.GetResponseStream();

StreamReader tReader = new StreamReader(dataStream);

String sResponseFromServer = tReader.ReadToEnd();

notification = sResponseFromServer;
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
catch (Exception ex)
{
notification.Status = false;
notification = "ERROR DESCRIPTION : " + ex.Message;
}

Answer

Try this hope this help you..........

try
        {
            var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            var senderId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            string deviceId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var data = new
            {
                to = deviceId,
                notification = new
                {
                    body = "This is the message Body",
                    title = "This is the title of Message",
                    icon = "myicon"
                },
                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);
                            Label3.Text = sResponseFromServer;
                        }
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }


    }