How to leave my app open waiting for the server to send information

2

I am creating a client app that as soon as it changes status of some component on the server the server will send the client so that the user can visualize. I need the app to be open receiving information and whenever user opens it should put your login, password and send to server to make first connection with this the app stays in the background and only close when the user will not use more and go in the option to leave placed in the app. I would like a hint of how I can do this, what should I search or some tutorial.

I want to do something similar to this app's template aNag

    
asked by anonymous 26.06.2014 / 13:08

1 answer

4

Guilherme,

Actually leaving apps and threads running in the background with open connections is not recommended. Imagine if every app had an open port and an established connection waiting for a response from the server? In a mobile environment where the 3G connection is often very slow, it would clog the network, and the user would not be able to navigate efficiently. So from there, Google has created a server bus that makes this communication to us, thus avoiding congestion in case the internet connection is too bad.

This bar is called GCM (Google Cloud Messaging), quoted in the comments by our friend Wakim.

It works as follows:

  • The app registers to your GCM account and a Key is generated

  • Your app should deliver this Key to your server that wants to communicate with the app

  • The server sends a notification to the GCM server with the key of your application

  • GCM queues notifications and delivers them to your mobile phone

  • Your phone's notification service delivers the notification to your code.

  • That is, the servers communicate with GCM and GCM with your cellphone, this means that the cell phone only needs an open connection: that of GCM. This allows me to scale and avoids connection problems. This is how facebook apps and whatsapp works. Your app does not need to stay open, and you do not need background threads listening for 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 on this HERE , with explanations of how to implement the customer part and the part of the server and more.

    You can also find a very well-explained video tutorial HERE .

    I hope I have helped !!!

        
    27.06.2014 / 15:27