GCM使用JSON发送到多个RegistrationID:没有通知到达

时间:2013-08-29 10:37:37

标签: c# android json google-cloud-messaging

我正在使用Android Google Cloud Messaging通过c#.net向设备发送通知。我得到了API Key,RegistrationID和senderID(第一种方法没有用,GCM的配置没有任何问题)。

这是我想要将消息发送到1个registrationID的第一种方法(检查postData,我不使用Json):

    public void SendPushNotification(string regID, string message)
 {


     {
         string GoogleAppID = "APIKey";
         var SENDER_ID = "SenderID";
         var value = message;
         WebRequest tRequest;
         tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
         tRequest.Method = "post";
         tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
         tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

         tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

         string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regID + "";
         Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
         tRequest.ContentLength = byteArray.Length;

         Stream dataStream = tRequest.GetRequestStream();
         dataStream.Write(byteArray, 0, byteArray.Length);
         dataStream.Close();

         WebResponse tResponse = tRequest.GetResponse();

         dataStream = tResponse.GetResponseStream();

         StreamReader tReader = new StreamReader(dataStream);

         String sResponseFromServer = tReader.ReadToEnd();

         HttpWebResponse httpResponse = (HttpWebResponse)tResponse;
         string statusCode = httpResponse.StatusCode.ToString();

         tReader.Close();
         dataStream.Close();
         tResponse.Close();
     }
        catch {
            throw new WebFaultException<string>("Error", HttpStatusCode.ServiceUnavailable);
        }

因此,在调用它时,设备会成功接收消息。

这是我的第二种方法,我使用Json格式发送到多个ID: (注意,该过程不将registrationID作为参数,因为我将它添加到我在代码中声明的列表中)

    public void SendPushNotification(string message)
 {

     string stringregIds = null;
     List<string> regIDs = new List<string>();
     //Here I add the registrationID that I used in Method #1 to regIDs
     //Then I use 
     stringregIds = string.Join("\",\"", regIDs);
     //To Join the values (if ever there are more than 1) with quotes and commas for the Json format below



     try
     {
         string GoogleAppID = "APIKey";
         var SENDER_ID = "SenderID";
         var value = message;
         WebRequest tRequest;
         tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
         tRequest.Method = "post";
         tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
         tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

         tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

         string postData = "{\"collapse_key\":\"score_update\",\"time_to_live\":108,\"delay_while_idle\":true,\"data\": { \"message\" : "+"\""+value+"\",\"time\": "+"\""+System.DateTime.Now.ToString()+"\"},\"registration_ids\":[\""+stringregIds+"\"]}";

         Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
         tRequest.ContentLength = byteArray.Length;

         Stream dataStream = tRequest.GetRequestStream();
         dataStream.Write(byteArray, 0, byteArray.Length);
         dataStream.Close();

         WebResponse tResponse = tRequest.GetResponse();

         dataStream = tResponse.GetResponseStream();

         StreamReader tReader = new StreamReader(dataStream);

         String sResponseFromServer = tReader.ReadToEnd();

         HttpWebResponse httpResponse = (HttpWebResponse)tResponse;
         string statusCode = httpResponse.StatusCode.ToString();

         tReader.Close();
         dataStream.Close();
         tResponse.Close();
     }
        catch {
            throw new WebFaultException<string>("Error", HttpStatusCode.ServiceUnavailable);
        }

方法不会提供任何错误或任何内容,但通知不会到达设备。 (我是console.writelined postData,它的格式与http://developer.android.com/google/gcm/http.html相同 所以我真的不知道要修复什么,同时向用户发送通知会更容易,而不是每隔X秒向每个用户发送1个。

感谢。

2 个答案:

答案 0 :(得分:2)

更改

Request.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";

Request.ContentType = "application/json";

回答这个问题。

他提供的JSON数据显然暗示了相反的情况。

HTTP标头必须包含以下标头:

  

Content-Type:JSON的application / json;   application / x-www-form-urlencoded; charset = UTF-8用于纯文本。

请参阅:https://developer.android.com/google/gcm/http.html

答案 1 :(得分:-5)

替换此行:

Request.ContentType = "application/json";