Ton
Ton

Reputation: 9736

GCM priority message doesn't wake up my app

I have my Android device optimizing my app. So my app is sleeping in the background but it is supposed to wake up if a priority GCM message is received. As staten 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.

and 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.

I use this code to send the priority messages from my C# server to the Android devices:

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;
}

But my app doesn't wake up. Even if I unlock the device.

I found out that once my device "blocks" my app for being on the optimized list then my app will not receive any more messages. It seems like the system is just killing the app completely and it will not receive any GCM message. I am using a Galaxy S4 with Lollipop. Any help?

Upvotes: 1

Views: 929

Answers (1)

Arthur Thompson
Arthur Thompson

Reputation: 9225

Plain text format does not support message priority. You need to use application/json format to use the priority field.

Upvotes: 1

Related Questions