2012-08-12 3 views
3

C#을 통해 gcm 메시지를 보내려고합니다.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> 

답변

1

등록 ID는 JSON 배열이 때문에 하나의 문자열 대신 목록을 사용해야합니다.

2

당신은 JSON 데이터

사용

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

대신

return "\"data\": {\"Message\": \"" + postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" + postFieldNameValue["registration_ids"] + "\"]}"; 
의 먼저 브래킷을 잊었다
관련 문제