0
首先,我不好意思問這個問題,因爲我認爲這個錯誤很簡單。谷歌雲消息傳遞 - 使用「delay_while_idle」 - 應用服務器C#
我在android上使用GCM,我用C#編寫了一個應用服務器。一切工作正常,但我有疑問試圖指定delay_while_idle參數。我說的是真的,但我不工作,我的意思是,如果我鎖定設備,gcm消息會立即到達。
這裏是我的代碼
private string SendNotification()
{
string result = string.Empty;
String GCM_URL = @"https://gcm-http.googleapis.com/gcm/send";
string collapseKey = DateTime.Now.ToString();
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("data.title", HttpUtility.UrlEncode("title"));
data.Add("data.description", HttpUtility.UrlEncode("description"));
StringBuilder sb = new StringBuilder();
sb.AppendFormat("registration_id={0}&collapse_key={1}", REGISTRATION_ID, collapseKey);
sb.AppendFormat("&delay_while_idle=true");
foreach (string item in data.Keys)
{
if (item.Contains("data."))
sb.AppendFormat("&{0}={1}", item, data[item]);
}
string msg = sb.ToString();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GCM_URL);
req.Method = "POST";
req.Headers.Add("Authorization:key=" + API_KEY);
req.ContentType = "application/x-www-form-urlencoded;;charset=UTF-8";
req.ContentLength = msg.Length;
using (System.IO.StreamWriter oWriter = new System.IO.StreamWriter(req.GetRequestStream()))
{
oWriter.Write(msg);
}
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
{
string respData = sr.ReadToEnd();
if (resp.StatusCode == HttpStatusCode.OK) // OK = 200
{
if (respData.StartsWith("id="))
{
result = "ok";
}
else
result = respData.ToString();
}
else if (resp.StatusCode == HttpStatusCode.InternalServerError || resp.StatusCode == HttpStatusCode.BadGateway) // 500
result = "Internal server error";
else if (resp.StatusCode == HttpStatusCode.ServiceUnavailable || resp.StatusCode == HttpStatusCode.BadGateway) // 503
result = "Server unavailable";
else if (resp.StatusCode == HttpStatusCode.Unauthorized) // 401
result = "invalid api key";
else
result = "Error: " + resp.StatusCode;
}
}
return result;
}
這是正確的,我使用的參數「delay_while_idle」的方式嗎? (我試過用「delay_while_idle = 1」,但相同)
也許,我使用了錯誤的格式
[更新] 讀GCM文檔,我看到了,我必須把它放在JSON格式
是的,這是我在GCM文檔上閱讀的,謝謝。 – AlphaDeveloper