Send Push Notification to ANDROID Devices

SEND PUSH NOTIFICATION TO ANDROID DEVICES


public static string APPSendNotification(string Title, string Text, string DeviceID, CSendNoti Data, int DeviceType = 1)
    {
        var result = "-1";
        try
        {

            if (!string.IsNullOrEmpty(DeviceID))
            {

                var webAddr = "https://fcm.googleapis.com/fcm/send";
                //VAISHALI GEMS
                //string SERVER_API_KEY = "SERVERAPIKEY";
                //string SENDER_ID = "SENDERID";
                string SERVER_API_KEY = "SERVERAPIKEY";
                string SENDER_ID = "SENDERID";

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Headers.Add("Authorization:key=" + SERVER_API_KEY);
                httpWebRequest.Headers.Add("Sender: id=" + SENDER_ID);
                httpWebRequest.Method = "POST";
                string d = "";
                if (Data != null)
                {
                    Data.title = Title;
                    Data.text = Text;
                    d = ConvertObjectToJSon(Data);
                }
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "";
                    if (DeviceType == 1)
                        json = "{\"to\": \"" + DeviceID + "\",  \"priority\" : \"high\", \"notification1\": { \"title\": \"" + Title + "\",\"body\": \"" + Text + "\" },\"data\":" + d + "}";
                    else
                        json = "{\"to\": \"" + DeviceID + "\",  \"priority\" : \"high\", \"notification\": { \"title\": \"" + Title + "\",\"body\": \"" + Text + "\" },\"data\":" + d + "}";

                    streamWriter.Write(json);
                    streamWriter.Flush();
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                    //Obj.ReturnCode = "1";
                    //Obj.ReturnMsg = result.ToString();
                    //Obj.ReturnValue = string.Empty;
                }
            }

        }
        catch (System.Exception ex)
        {
            return ex.ToString();
        }
        return result;
    }

Comments

Popular posts from this blog

Convert Html Code to Image JPG PNG in asp.net C#