Skip to main content

send push notification from C# using FCM

        public bool SendPushNotification(string deviceId, string message)
        {
     //applicationID = "AIzaSyxxxxxxxxxxxxxxxxxxx"
    // senderId = "98xxxxxxxxx"
            bool isSent = false;
            string str = "";
            try
            {

                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 = message,
                        title = "SpAlert",
                        sound = "Enabled"

                    }
                };
                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(data);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add(string.Format("Authorization: key={0}", utilityHelper.PushNotificationAppId));
                tRequest.Headers.Add(string.Format("Sender: id={0}", utilityHelper.PushNotificationSenderID));
                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();
                                str = sResponseFromServer;
                                Console.WriteLine(sResponseFromServer);
                                isSent = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                str = ex.Message;
            }
            return isSent;
        }

Comments

Popular posts from this blog

Change Key Dynamically in Web.Config

System.Xml; private void SetConfigSettings() { string path = Server.MapPath("Web.config"); string newConnectionString = @"Server=local;Database="+txtDatabaseName.Text+";Trusted_Connection=true"; XmlDocument xDoc = new XmlDocument(); xDoc.Load(path); XmlNodeList nodeList = xDoc.GetElementsByTagName("appSettings"); XmlNodeList nodeAppSettings = nodeList[0].ChildNodes; XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes; xmlAttCollection[0].InnerXml = txtKey.Text; // for key attribute xmlAttCollection[1].InnerXml = newConnectionString; // for value attribute xDoc.Save(path); // saves the web.config file }

HTTP errors

HTTP errors are sent to your web browser from a website if a problem is encountered when trying to view a webpage. If the webpage cannot be displayed, Internet Explorer will display either the actual error page sent by the website or a friendly error message built into Internet Explorer. Below you will find some of the most common errors and ideas for how to solve the problem that's causing them. The following table lists the most common HTTP errors that Internet Explorer will display. For information about HTTP protocols, error codes, and causes, visit the World Wide Web Consortium (W3C) website. You can also search the web for specific error codes. HTTP error message What it means What you can do The webpage cannot be found (HTTP 400) Internet Explorer is able to connect to the web server, but the webpage cannot be found because of a problem with the web address (URL). This error message often happen...