0
我正在使用c#web服務將註冊ID傳遞給GCM。我有兩個Android手機,我有有效的註冊ID。當我通過他們單獨GCM不會返回任何錯誤和設備通知。但是,當我嘗試將它們作爲JSON傳遞一起我得到錯誤爲"Error = missing registration".
GCM返回缺少註冊錯誤
正在關注是我的web服務的是經過registtration 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;
}
我是新來的發展,可能失去了一些東西很簡單。任何幫助是提前appreciated..Thanks
Thx ..但我得到了一個解決方案..我在一個循環內個別地註冊每個註冊ID ..上面顯示的格式是正確的,但我無法形成,因爲我必須從數據庫中取得註冊ID ..這是我的代碼,它可以成功運行.. – 2013-04-22 13:42:43