예제 #1
0
        public async Task <GCMResult> SendMessageAsync(string authKey, string[] registrationIds, IDictionary <string, string> content)
        {
            using (var client = new HttpClient())
            {
                var jsonObject = new GCMSendJson
                {
                    RegistrationIds = registrationIds,
                    Data            = content
                };
                string json    = JsonConvert.SerializeObject(jsonObject);
                var    request = new HttpRequestMessage(HttpMethod.Post, "https://android.googleapis.com/gcm/send")
                {
                    Content = new StringContent(json, Encoding.UTF8, "application/json"),
                };
                ServicePointManager.ServerCertificateValidationCallback += ValidationCallback;
                request.Headers.TryAddWithoutValidation(HttpRequestHeader.Authorization.ToString(), string.Format("key={0}", authKey));
                var res = await client.SendAsync(request);

                var           responseCode = res.StatusCode;
                GCMResultJson result       = null;
                if (responseCode.Equals(HttpStatusCode.OK))
                {
                    var contentResult = await res.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <GCMResultJson>(contentResult);
                }

                return(new GCMResult
                {
                    ResponseCode = responseCode,
                    ResultJson = result
                });
            }
        }
예제 #2
0
        public GCMResult SendMessage(string authKey, string[] registrationIds, IDictionary <string, string> content)
        {
            var jsonObject = new GCMSendJson
            {
                RegistrationIds = registrationIds,
                Data            = content
            };
            string json     = JsonConvert.SerializeObject(jsonObject);
            var    encoding = new UTF8Encoding();

            byte[] postData = encoding.GetBytes(json);

            var url = new Uri("https://android.googleapis.com/gcm/send");

            ServicePointManager.ServerCertificateValidationCallback += ValidationCallback;

            var conn = (HttpWebRequest)WebRequest.Create(url);

            conn.Proxy = null;

            conn.Method        = "POST";
            conn.ContentType   = "application/json;charset=UTF-8";
            conn.ContentLength = postData.Length;
            conn.Headers.Add("Authorization", string.Format("key={0}", authKey));
            var sort = conn.GetRequestStream();

            sort.Write(postData, 0, postData.Length);
            sort.Close();

            var           httpWebResponse = ((HttpWebResponse)conn.GetResponse());
            var           responseCode    = httpWebResponse.StatusCode;
            GCMResultJson jsonResult      = null;

            if (responseCode.Equals(HttpStatusCode.OK))
            {
                var streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                var errorString  = streamReader.ReadToEnd();
                jsonResult = JsonConvert.DeserializeObject <GCMResultJson>(errorString);
            }

            return(new GCMResult
            {
                ResponseCode = responseCode,
                ResultJson = jsonResult
            });
        }