我正在使用c#webservices将注册ID传递给GCM。我有两个Android手机,我有有效的注册ID。当我单独传递它们时GCm不会返回任何错误并通知设备。但是当我尝试将它们作为JSON一起传递时我收到错误"Error = missing registration".
< / p>
以下是我的网络服务代码,它将注册ID传递给GCM ..
public string Notify(string deviceId, string 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(HttpRequestHeader.Authorization, "key=AIzaSyCUJX_f_NZzYbigWMDK9U5vkzZ9mPiK8jA");
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
int count = 2;
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["Login"]);
conn.Open();
string send_notification = "select RegId from REG_ID ";//where RegId <> '"+deviceId+"'";
SqlCommand cmd2 = new SqlCommand(send_notification, conn);
DataSet ds2 = new DataSet();
SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2);
adapter2.Fill(ds2);
int count1 = ds2.Tables[0].Rows.Count;
registration[] r = new registration[count1];
for (int i = 0; i < count1; i++)
{
r[i] = new registration();
r[i].registration_ids = ds2.Tables[0].Rows[i]["RegId"].ToString();
}
JavaScriptSerializer js = new JavaScriptSerializer();
string strJson = js.Serialize(r);
string postData = /*"collapse_key=score_update&time_to_live=108&delay_while_idle=1&*/"data.message=" + message + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id= "+ deviceId + "";
Console.WriteLine(strJson);
Byte[] byteArray = Encoding.UTF8.GetBytes(strJson);
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();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
我是开发新手,可能会遗漏一些非常简单的东西。任何帮助表示赞赏。谢谢提前
答案 0 :(得分:2)
您的JSON应如下所示:
{ "data": {
"score": "5x1",
"time": "15:10"
},
"registration_ids": ["4", "8", "15", "16", "23", "42"]
}
application/json
。strJson
包含哪些内容,所以我不能说它是否有效。postData
变量似乎属于纯文本格式,但无论如何都不使用它。