1

我的Android設備正在優化我的應用程序。所以我的應用程序正在後臺休眠,但如果收到優先GCM消息,它應該醒來。 as staten hereGCM優先消息不會喚醒我的應用程序

高優先級。 GCM嘗試立即傳送高優先級消息 ,允許GCM服務在 可能的情況下喚醒睡眠設備,並打開到您的應用服務器的網絡連接。具有 即時消息,聊天或語音呼叫警報的應用程序(例如,通常爲 )需要打開網絡連接,並確保GCM毫不延遲地將 消息傳送到設備。

here

GCM被優化以通過裝置的高優先級消息GCM的 打盹和應用待機空閒模式工作。即使用戶的 設備處於打盹模式或應用程序處於App Standby模式,GCM高優先級消息也可讓您可靠地喚醒您的應用以訪問網絡。在待機模式下,系統傳遞消息並給予應用程序 臨時訪問網絡服務和部分喚醒鎖,然後 將設備或應用程序返回到空閒狀態。

我用這個代碼從我的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消息。我正在使用帶有棒棒糖的Galaxy S4。任何幫助?

+0

我發現有人在這裏有我同樣的問題:http://developer.samsung.com/forum/board/thread/view.do?boardName=General&messageId=286651&frm=7&tagValue=smartmanager&curPage=1 – Ton

回答

1

純文本格式不支持消息優先級。您需要使用application/json格式來使用優先級字段。

+0

謝謝。你能提供任何指出的鏈接嗎? – Ton

+0

查看參考文檔,純文本參考不包含優先選項。 https://developers.google.com/cloud-messaging/http-server-ref#send-downstream –

+0

嗯,我不知道我是否看錯了地方,但我看到有一個「優先」字段在這兩個協議(HTTP協議和XMPP協議) – Ton

相關問題