Exemple #1
0
        public ActionResult ActionMessGoogle(tblGoogleMessage objSubmit)
        {
            objSubmit.DateCreated = DateTime.Now;
            var          rnd            = new Random();
            var          tick           = rnd.Next(1, int.MaxValue);
            const string uri            = "https://fcm.googleapis.com/fcm/send";
            var          myParameters   = "{\"to\": \"/topics/info\",\"data\": {\"id\" : " + tick + ",\"title\": \"" + objSubmit.Title + "\",\"content-text\": \"" + objSubmit.ContentText + "\"}}";
            var          httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

            httpWebRequest.ContentType = "application/json; charset=UTF-8";
            httpWebRequest.Headers[HttpRequestHeader.Authorization] = "key=AIzaSyDlUtC0lD4ndwv4f8T62N52yvgUURzYtE8";
            httpWebRequest.Method    = "POST";
            httpWebRequest.Accept    = "application/json";
            httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.3";
            byte[] postBytes = Encoding.UTF8.GetBytes(myParameters);

            var requestStream = httpWebRequest.GetRequestStream();

            // now send it
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var htmlResult = streamReader.ReadToEnd();
                var jObject    = JObject.Parse(htmlResult);

                var messageId = (string)jObject.SelectToken("message_id");
                objSubmit.GoogleId = messageId;
            }
            try
            {
                supportRepository.AddGoogleMess(objSubmit);
            }
            catch (Exception)
            {
                return(Json(new
                {
                    Error = true
                }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new
            {
                Error = false
            }, JsonRequestBehavior.AllowGet));
        }