Navigation

Wednesday 15 July 2020

Send Ionic Push Notification to Android App by C# using FCM







Step 1: Create an application in asp.net with C# and paste the following code.


 using System;

 namespace WebToSendIonicNotic
 {
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
       
        protected void btnSend_Click(object sender, EventArgs e)
        {
            string devicePushToken = "et7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsX";
            string Message = txtMessage.Text;
           
            //Start sending push notification to apps
            PushNotification fcmPush = new PushNotification();
            PushNotification result = fcmPush.SendNotification(devicePushToken,"Notification title", Message, "news");
            //End push notification
        }
    }

 }


 Step 2: Create a class with the name PushNotification and paste the following code. You can get YOUR_FCM_SERVER_API_KEY and YOUR_FCM_SENDER_ID at console firebase, for more details click here


 using System;
 using System.IO;
 using System.Net;
 using System.Text;
 using System.Web.Script.Serialization;

 namespace WebToSendIonicNotic
 {
    public class PushNotification
    {
        public PushNotification()
        {
         
        }

        public bool Successful
        {
            get;
            set;
        }

        public string Response
        {
            get;
            set;
        }
        public Exception Error
        {
            get;
            set;
        }
        public PushNotification SendNotification(string YOUR_FCM_DEVICE_ID, string _title,                                                     string _message, string _topic)
        {
            string YOUR_FCM_SERVER_API_KEY = "TWI7pb0DYxxxxxxxxxxxxxxxxe9S1Oq1KFzd4NkNZDQH";
            string YOUR_FCM_SENDER_ID = "xxxxxxxxxxxx";
          
            PushNotification result = new PushNotification();
            try
            {
                result.Successful = true;
                result.Error = null;              
                var requestUri = "https://fcm.googleapis.com/fcm/send";

                WebRequest webRequest = WebRequest.Create(requestUri);
                webRequest.Method = "POST";
                webRequest.Headers.Add(string.Format("Authorization: key={0}", YOUR_FCM_SERVER_API_KEY));
                webRequest.Headers.Add(string.Format("Sender: id={0}", YOUR_FCM_SENDER_ID));
                webRequest.ContentType = "application/json";

                var data = new
                {
                     to = YOUR_FCM_DEVICE_ID, //If you want to test for single device.
                    //to = "/topics/" + _topic, // this is for topic
                    notification = new
                    {
                        title = _title,
                        body = _message,
                        //icon="myicon"
                    }
                };
                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(data);

                Byte[] byteArray = Encoding.UTF8.GetBytes(json);

                webRequest.ContentLength = byteArray.Length;
                using (Stream dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);

                    using (WebResponse webResponse = webRequest.GetResponse())
                    {
                        using (Stream dataStreamResponse = webResponse.GetResponseStream())
                        {
                            using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                                String sResponseFromServer = tReader.ReadToEnd();
                                result.Response = sResponseFromServer;
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                result.Successful = false;
                result.Response = null;
                result.Error = ex;
            }
            return result;
        }

    }

 }

5 comments:

  1. So lot to occur over your amazing blog. Your blog procures me a fantastic transaction of enjoyable.. Salubrious lot beside the scene. visit this page

    ReplyDelete
  2. At this point you'll find out what is important, it all gives a url to the appealing page: check this site

    ReplyDelete
  3. That's amazing informative post, I want to add one more thing, If you want to make your web visitor to your subscriber then you should definitely check gravitec lifetime deal Best push notification for website ever.

    ReplyDelete