William,
Actually letting apps and threads run in the background with open connections is not recommended. Can you imagine if every app had an open port and an established connection waiting for a response from the server? In a mobile environment where many times the 3G connection is very slow, it would jam the network, and the user would not be able to navigate efficiently. So, from there, Google created a server bus that makes this communication to us, thus avoiding congestion in case the internet connection is very bad.
This bar is called GCM (Google Cloud Messaging), quoted in comments by our friend Wakim.
It works as follows:
The app registers to your GCM account and a Key is generated
your app must deliver this Key to your server that you want to communicate with the app
The server sends a notification to the GCM servers with the key of your application
GCM queues notifications and delivers them to your phone
Your mobile notification service delivers the notification to your code.
That is, the servers communicate with GCM and GCM with your phone, this makes the phone only need a connection to be open: the GCM. This allows me to scale and avoids connection problems. This is how facebook and Whatsapp apps work. Your app doesn’t need to stay open, and doesn’t need background threads listening to connections, you just need to set up a service in your app and the operating system does the rest.
Obviously my explanation is very brief, and very superficial. You can find more details about this HERE, with explanations on how to implement the client part and the server part and more.
You can also find a very well explanatory video tutorial HERE.
I hope I’ve helped!!!
Guilherme, take a look at google’s GCM. You don’t even have to leave the app open to be notified.
– Wakim
This can be done with webSockets, and depends on the server technology used. Take a look at this link to understand the technology and Is another link for an Android component with webSockets support
– Caputo