GCM 优先级消息不会唤醒我的应用程序
GCM priority message doesn't wake up my app
我的 Android 设备正在优化我的应用程序。所以我的应用程序在后台休眠,但如果收到优先级 GCM 消息,它应该会被唤醒。正如史坦顿 here:
High priority. GCM attempts to deliver high priority messages
immediately, allowing the GCM service to wake a sleeping device when
possible and open a network connection to your app server. Apps with
instant messaging, chat, or voice call alerts, for example, generally
need to open a network connection and make sure GCM delivers the
message to the device without delay.
和here:
GCM is optimized to work with Doze and App Standby idle modes by means
of high-priority GCM messages. GCM high-priority messages let you
reliably wake your app to access the network, even if the user’s
device is in Doze or the app is in App Standby mode. In Doze or App
Standby mode, the system delivers the message and gives the app
temporary access to network services and partial wakelocks, then
returns the device or app to idle state.
我使用此代码将优先级消息从我的 C# 服务器发送到 Android 设备:
private string SendMessageUsingGCM(String sRegistrationId, string sTextToSend, string sCollapseKey)
{
String GCM_URL = @"https://gcm-http.googleapis.com/gcm/send";
bool flag = false;
string sError = "";
StringBuilder sb = new StringBuilder();
sb.AppendFormat("registration_id={0}&collapse_key={1}", sRegistrationId, sCollapseKey);
sb.AppendFormat("&delay_while_idle=0&priority=high");
sb.AppendFormat("&data.msg=" + HttpUtility.UrlEncode(sTextToSend)); //Para poder enviar caracteres especiales como ä, ë, arábigos...
string msg = sb.ToString();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GCM_URL);
req.Method = "POST";
req.ContentLength = msg.Length;
req.ContentType = "application/x-www-form-urlencoded";
req.Timeout = 20000;
req.Headers.Add("Authorization:key=" + MyAthorizationKey);
try
{
using (StreamWriter oWriter = new StreamWriter(req.GetRequestStream()))
{
oWriter.Write(msg);
}
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string respData = sr.ReadToEnd();
if (resp.StatusCode == HttpStatusCode.OK) // OK = 200
{
if (respData.StartsWith("id="))
flag = true;
else
sError = respData;
}
else if (resp.StatusCode == HttpStatusCode.InternalServerError) // 500
sError = "Internal server error. Try later.";
else if (resp.StatusCode == HttpStatusCode.ServiceUnavailable) // 503
sError = "Server not available temnporatily. Try later.";
else if (resp.StatusCode == HttpStatusCode.Unauthorized) // 401
sError = "The API Key is not valid.";
else
sError = "Error: " + resp.StatusCode;
}
}
}
catch (WebException e)
{ //The remote server returned an error: (502) Bad Gateway. //Más info:
//The remote server returned an error: (500) Internal Server Error. Más info:
sError = "WebException: " + e.ToString();
}
catch (Exception e)
{
sError = "Exception: " + e.ToString();
}
if (flag == true)
return "Ok";
return "Error " + sError;
}
但是我的应用没有唤醒。即使我解锁了设备。
我发现一旦我的设备 "blocks" 我的应用进入优化列表,我的应用将不会再收到任何消息。看起来系统只是完全杀死了应用程序,它不会收到任何 GCM 消息。我正在使用带 Lollipop 的 Galaxy S4。有帮助吗?
纯文本格式不支持消息优先级。您需要使用 application/json 格式才能使用优先级字段。
我的 Android 设备正在优化我的应用程序。所以我的应用程序在后台休眠,但如果收到优先级 GCM 消息,它应该会被唤醒。正如史坦顿 here:
High priority. GCM attempts to deliver high priority messages immediately, allowing the GCM service to wake a sleeping device when possible and open a network connection to your app server. Apps with instant messaging, chat, or voice call alerts, for example, generally need to open a network connection and make sure GCM delivers the message to the device without delay.
和here:
GCM is optimized to work with Doze and App Standby idle modes by means of high-priority GCM messages. GCM high-priority messages let you reliably wake your app to access the network, even if the user’s device is in Doze or the app is in App Standby mode. In Doze or App Standby mode, the system delivers the message and gives the app temporary access to network services and partial wakelocks, then returns the device or app to idle state.
我使用此代码将优先级消息从我的 C# 服务器发送到 Android 设备:
private string SendMessageUsingGCM(String sRegistrationId, string sTextToSend, string sCollapseKey)
{
String GCM_URL = @"https://gcm-http.googleapis.com/gcm/send";
bool flag = false;
string sError = "";
StringBuilder sb = new StringBuilder();
sb.AppendFormat("registration_id={0}&collapse_key={1}", sRegistrationId, sCollapseKey);
sb.AppendFormat("&delay_while_idle=0&priority=high");
sb.AppendFormat("&data.msg=" + HttpUtility.UrlEncode(sTextToSend)); //Para poder enviar caracteres especiales como ä, ë, arábigos...
string msg = sb.ToString();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GCM_URL);
req.Method = "POST";
req.ContentLength = msg.Length;
req.ContentType = "application/x-www-form-urlencoded";
req.Timeout = 20000;
req.Headers.Add("Authorization:key=" + MyAthorizationKey);
try
{
using (StreamWriter oWriter = new StreamWriter(req.GetRequestStream()))
{
oWriter.Write(msg);
}
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string respData = sr.ReadToEnd();
if (resp.StatusCode == HttpStatusCode.OK) // OK = 200
{
if (respData.StartsWith("id="))
flag = true;
else
sError = respData;
}
else if (resp.StatusCode == HttpStatusCode.InternalServerError) // 500
sError = "Internal server error. Try later.";
else if (resp.StatusCode == HttpStatusCode.ServiceUnavailable) // 503
sError = "Server not available temnporatily. Try later.";
else if (resp.StatusCode == HttpStatusCode.Unauthorized) // 401
sError = "The API Key is not valid.";
else
sError = "Error: " + resp.StatusCode;
}
}
}
catch (WebException e)
{ //The remote server returned an error: (502) Bad Gateway. //Más info:
//The remote server returned an error: (500) Internal Server Error. Más info:
sError = "WebException: " + e.ToString();
}
catch (Exception e)
{
sError = "Exception: " + e.ToString();
}
if (flag == true)
return "Ok";
return "Error " + sError;
}
但是我的应用没有唤醒。即使我解锁了设备。
我发现一旦我的设备 "blocks" 我的应用进入优化列表,我的应用将不会再收到任何消息。看起来系统只是完全杀死了应用程序,它不会收到任何 GCM 消息。我正在使用带 Lollipop 的 Galaxy S4。有帮助吗?
纯文本格式不支持消息优先级。您需要使用 application/json 格式才能使用优先级字段。