【问题标题】:GCM priority message doesn't wake up my appGCM 优先级消息不会唤醒我的应用
【发布时间】:2016-03-14 11:21:39
【问题描述】:

我的 Android 设备正在优化我的应用。所以我的应用程序正在后台休眠,但如果收到优先级 GCM 消息,它应该会唤醒。如声明here

高优先级。 GCM 尝试传递高优先级消息 立即,允许 GCM 服务唤醒睡眠设备 可能并打开到您的应用服务器的网络连接。应用程序 即时消息、聊天或语音呼叫警报,例如,通常 需要打开网络连接并确保 GCM 提供 立即向设备发送消息。

here:

GCM 经过优化,可通过以下方式在 Doze 和 App Standby 空闲模式下工作 高优先级 GCM 消息。 GCM 高优先级消息让你 可靠地唤醒您的应用程序以访问网络,即使用户的 设备处于打盹或应用处于应用待机模式。在打瞌睡或应用程序中 待机模式,系统下发消息给app 临时访问网络服务和部分唤醒锁,然后 将设备或应用返回到空闲状态。

我使用此代码将优先级消息从我的 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: http://stackoverflow.com/questions/2159361/error-502-bad-gateway-when-sending-a-request-with-httpwebrequest-over-ssl
        //The remote server returned an error: (500) Internal Server Error. Más info: http://stackoverflow.com/questions/4098945/500-internal-server-error-at-getresponse
        sError = "WebException: " + e.ToString();
    }
    catch (Exception e)
    {
        sError = "Exception: " + e.ToString();
    }


    if (flag == true)
        return "Ok";

    return "Error " + sError;
}

但我的应用没有唤醒。即使我解锁了设备。

我发现,一旦我的设备“阻止”我的应用在优化列表中,那么我的应用将不会再收到任何消息。似乎系统只是完全杀死了应用程序,它不会收到任何 GCM 消息。我正在使用带有 Lollipop 的 Galaxy S4。有什么帮助吗?

【问题讨论】:

标签: android google-cloud-messaging android-doze-and-standby


【解决方案1】:

纯文本格式不支持消息优先级。您需要使用 application/json 格式来使用优先级字段。

【讨论】:

  • 谢谢。你能提供任何说明这一点的链接吗?
  • 查看参考文档,纯文本参考不包括优先级选项。 developers.google.com/cloud-messaging/…
  • 好吧,我不知道我是不是看错地方了,但我看到两个协议(HTTP 协议和 XMPP 协议)中都有一个“优先级”字段
  • 抱歉。我刚刚发现有3种方法。 1:下游 HTTP 消息 (JSON)。 2:下游 HTTP 消息(纯文本)。 3:下游 XMPP 消息 (JSON)。我想我正在使用数字 2,它是唯一一个不允许“优先级”(我运气不好),我也无法在网上找到数字 1 的清晰简单示例
猜你喜欢
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
相关资源
最近更新 更多