发送gcm消息的错误请求

时间:2012-08-12 12:16:09

标签: android google-cloud-messaging

我正在尝试通过C#发送gcm消息。

我尝试过几次,但在尝试使用json方法发送时,我得到了http:400-Bad request

当我尝试用文字发送时,我无法阅读它(rtl语言) - 这就是我尝试JSON的原因。

谁知道有什么问题? 谢谢!

    private static string SendNotificationJson2(string id, string msg)
    {
            var AuthString = "AIzaSyDAtmaqSdutBQemqmd4dQgf33B_6ssbvXA";
            var RegistrationID = id;
            var Message = msg;


        //-- Create GCM request insted of C2DM Web Request Object --//
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");

        Request.Method = "POST";
        Request.KeepAlive = false;

        //-- Create Query String --//
        Dictionary<String, String> dict = new Dictionary<string, string>();
        dict.Add("registration_ids", RegistrationID);
        dict.Add("data", Message);
        dict.Add("collapse_key", "1");

        string postData = GetPostStringFrom(dict);
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        Request.ContentType = "application/json";
        Request.ContentLength = byteArray.Length;


        Request.Headers.Add("Authorization", "key=" + AuthString);

        //-- Delegate Modeling to Validate Server Certificate --//
        ServicePointManager.ServerCertificateValidationCallback += delegate(
                    object
                    sender,
                    System.Security.Cryptography.X509Certificates.X509Certificate
                    pCertificate,
                    System.Security.Cryptography.X509Certificates.X509Chain pChain,
                    System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
        {
            return true;
        };

        //-- Create Stream to Write Byte Array --// 
        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        //-- Post a Message --//
        WebResponse Response = Request.GetResponse();
        HttpStatusCode 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";
            //Console.WriteLine("Response from web service not OK :");
            //Console.WriteLine(((HttpWebResponse)Response).StatusDescription);
        }

        StreamReader Reader = new StreamReader(Response.GetResponseStream());
        string responseLine = Reader.ReadLine();
        Reader.Close();

        return "ok";
    }

    private static string GetPostStringFrom(Dictionary<string,string> postFieldNameValue)
    {
        // return Newtonsoft.Json.JsonConvert.SerializeObject(postFieldNameValue);
        return "\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";
    }</code>

2 个答案:

答案 0 :(得分:2)

您忘记了Json数据中的第一个括号

使用

var vacExtList = vacList.Select(vac => new SelectVacancyDetails_ResultExtend(vac))
                        .ToList();

而不是

return "{\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";

答案 1 :(得分:1)

您的注册ID必须是JSON数组,因此请使用列表而不是单个字符串。