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

Asked

Viewed 2,236 times

2

I’m creating a client app that once it changes the status of some component on the server the server will send to the client so that the user can view it. I need the app to be open by receiving information and whenever the user opens it to put their login, password and send to server to make first connection with it the app is in the background and only close when the user will not use more and go on the exit option placed in the app. I would like a tip on how to do this, what should I search for or some tutorial.

I intend to do something similar to the model of this app aNag

  • Guilherme, take a look at google’s GCM. You don’t even have to leave the app open to be notified.

  • 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

1 answer

4


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:

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

  2. your app must deliver this Key to your server that you want to communicate with the app

  3. The server sends a notification to the GCM servers with the key of your application

  4. GCM queues notifications and delivers them to your phone

  5. 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!!!

  • Thank you very much! helped I am studying the GCM to be able to implement it correctly. and vlw by clarification

Browser other questions tagged

You are not signed in. Login or sign up in order to post.