1

I have three applications. A Content Management System developed using Asp.NET MVC, an Asp.NET Web API application and an Android Application. I have used SQL Server 2016 as my database. The Content Management System and the Android Application performs database operations via the Web API.

Now I need to implement push notifications in my Android Application. Whenever the administrator post new data from the Content Management System, the android application user is supposed to get a push notification in their phone.

How do I implement this? I went through various tutorials online. They have implemented using Firebase. I understood that bit. But they have also used php scripts and WAMP server for the implementation. I am not familiar with php.

Can some one tell me how can I implement push notifications in my scenario without using php? I'd appreciate the help.

Thank you

vinitpradhan18
  • 51
  • 1
  • 10

1 Answers1

0

ASP side using FCM

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);
        }


    }

andrroid side usinf FCM follow the link

https://www.codementor.io/flame3/send-push-notifications-to-android-with-firebase-du10860kb

Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
  • The guide from the link worked. But I'm still not sure where to place the above ASP code. I tried placing in the Post method in my Web API. But I'm not sure how it works. And also how do I generate Device Id and Sender Id. I'm new at this so kindly pardon my ignorance. – vinitpradhan18 Jun 11 '17 at 15:13
  • you can generate a device id in android class ,you can send that device id in login time - find token id in android class follow the tutorial , – Jai Khambhayta Jun 12 '17 at 04:01