Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FCM with Asp.net web api 2

I have built a web api which will be the back-end side of AngularJs, IOS, and Android front-ends applications.

Now I have requirement to push notifications from my web api to front-end apps when for example a product has been updated.

I was thinking of using SignalR to push notifications in realtime manner but it will not be useful if other users are offline.

Now I am planning to use FCM to push notifications so you could you please give me answer for my question

How to integrate my web api with FCM and what are the benefits I would get from using FCM in pushing notifications?

PS

I would appreciate any references to integrate asp.net web api with FCM

like image 940
Simple Code Avatar asked Feb 09 '18 12:02

Simple Code


Video Answer


1 Answers

Lets create console application as following:

    class Program
{
    static void Main(string[] args)
    {
        string resend ;
        do
        {
            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var objNotification = new
            {
                to = "Token the device you want to push notification to",
                data = new
                {
                    title = "title",
                    body = "body",
                    icon = "/firebase-logo.png"
                }
            };
            string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);

            Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
            tRequest.Headers.Add(string.Format("Authorization: key={0}", "your authorization key"));
            tRequest.Headers.Add(string.Format("Sender: id={0}", "your senderId"));
            tRequest.ContentLength = byteArray.Length;
            tRequest.ContentType = "application/json";
            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 responseFromFirebaseServer = tReader.ReadToEnd();

                            FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
                            if (response.success == 1)
                            {

                                Console.WriteLine("succeeded");
                            }
                            else if (response.failure == 1)
                            {
                               Console.WriteLine("failed");

                            }

                        }
                    }

                }
            }

            resend = Console.ReadLine();
        } while (resend == "c");
    }

}

public class FCMResponse
{
    public long multicast_id { get; set; }
    public int success { get; set; }
    public int failure { get; set; }
    public int canonical_ids { get; set; }
    public List<FCMResult> results { get; set; }
}
public class FCMResult
{
    public string message_id { get; set; }
}
like image 57
Simple Code Avatar answered Sep 22 '22 17:09

Simple Code