title: Android persistent socket connection rules
body: <p>I have been doing some testing for a custom push notification solution for Android devices using persistent sockets. I would like to share my findings and validate the results.</p>
  
  <p><b>Simple Description</b><br/>
  The applications runs a foreground service and establishes a connection with the server and maintains that connection via aggressive pinging (@ 10 secs interval). If the connection is ever detected as dead, the app keeps trying to reconnect indefinitely. The server sends notifications via duplex channel.</p>
  
  <p><strong>Test 1 :</strong></p>
  
  <pre><code>Pinging is done using a timer at 10 second intervals.
  Server sends notification every minute.
  Applications acquires wifi and wake locks.
  Duration : 8 hours
  Battery loss : ~14%
  </code></pre>
  
  <p><strong>Test 2 :</strong></p>
  
  <pre><code>Pinging is done using AlarmManager at 10 second intervals.
  Server sends notification every minute.
  Application acquires only a wifilock
  Duration : 8 hours
  Battery loss : ~7%
  </code></pre>
  
  <p><strong>Assumptions:</strong> An incoming network packet automatically wakes up the CPU, thus no need for a wake lock. Using AlarmManager to ping(instead of timers) means we do not need a wakelock. </p>
  
  <p>Removing that wakelock really seemed to help the battery. Surprisingly, the aggressive pinging on either solution did not affect the battery life as much as I would have expected. (We had many other tests including one where the application just held a wifilock and did nothing which caused around 4% to 5% battery loss over the same period)</p>
  
  <p>Since the application was able to successfully send all the ping requests and receive all the incoming messages, I believe my assumptions are correct. But I would love to get some confirmation from any experts.</p>
  
  <p><strong>One more question:</strong>
  If the application was to instead listen for incoming connections. I would need to hold a wakelock in this case, correct? An incoming connection does not wake up the CPU? We are not going down this route, but just wanted to confirm.</p>
  
  <p>Also, please do not recommend GCM, it has been ruled out by company policy.</p>
  
  <p>Thanks.</p>