1

我想向 GCM 发送消息,但收到状态为 200 的不可用。我做错了什么?

var GoogleAppID = ""; var DeviceID = "";

            var request = WebRequest.Create("https://android.googleapis.com/gcm/send");
            request.Method = "POST";
            request.Headers.Add("Authorization", "key=" + GoogleAppID);
            request.ContentType = "application/json";

            var message = new Message();
            message.registration_ids.Add(DeviceID);
            message.data.Add("gcm_message", "12345");
            var jsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(message);
            byte[] byteArray = Encoding.UTF8.GetBytes(jsonMessage);
            request.ContentLength = byteArray.Length;

            var reqStream = request.GetRequestStream();
            reqStream.Write(byteArray, 0, byteArray.Length);

            var response = request.GetResponse();
            var responseCode = ((HttpWebResponse)response).StatusCode;
            if (responseCode.Equals(HttpStatusCode.Unauthorized) || responseCode.Equals(HttpStatusCode.Forbidden))
            {
                return "Unauthorized - need new token";

            }
            else if (!responseCode.Equals(HttpStatusCode.OK))
            {
                return "Response from web service isn't OK";
            }

            var responseStream = response.GetResponseStream();
            var streamReader = new StreamReader(responseStream);
            var responseContent = streamReader.ReadToEnd();
            streamReader.Close();
4

1 回答 1

0

不需要自己建库,github已经有了。使用以下内容。

https://github.com/Redth/GCMSharp

于 2012-12-05T10:31:33.303 回答