title: Implementing android chat module using long polling
body: <p>I am writing an Android app that among features has a chat module. I have choose to implement the chat using long polling.</p>
  
  <p>Because I am trying to avoid eating up the user phone battery I thought about adding to the implementation a GCM messages platform to notify the device about new messages.</p>
  
  <p>The idea is to use long polling while the user has the app open on his screen so engaging in a real time chat would be fast and does not involve sending a lot of GCM messages, while when idle, the server will know that the user is not currently polling and will send the device a GCM message to wake it up when a new message arrives.</p>
  
  <p>I am struggling with a few issues implementing this method:</p>
  
  <ol>
  <li><p>What is the best way to know the status of the user - Is hes polling right now or waiting for the push message to wake it up. Should I set the app to notify the server when it goes to sleep or the server should detect it by itself?</p></li>
  <li><p>If I set it so the app will notify the server when to switch between modes, how do I recover in cases this notification did not got through. (when the client is waiting for gcm and the server thinks it's polling and will not send one..)</p></li>
  <li><p>I experienced some case where the client was polling and the server did not responded even when it got new messages. Maybe some kind of timeout on the server that the client is not aware of... If that's a bug I'll fix it, but what can I do in cases when the client thinks it's polling while the server does not know about it (the timeout of the requests is too long to wait for the next poll).</p></li>
  </ol>
  
  <p>My client is native Android and the Webservice is WCF (C#).</p>
  
  <p>Any thought will help. </p>