0

I have an application with list of data that I get from server with http request. Now I want to make a notification when new data is available and I assume that it can be achieved with service.

Is that a good practice? Is there any limitations for number of requests made from service?

What I want to achieve is something like gmail application. When I get a new email, notification is shown.

I want my app to be as up to date with data as possible, but I understand that making requests every 5 seconds might be too much.

I am open to all alternatives and various ideas how to do that.

Uroš Podkrižnik
  • 8,607
  • 5
  • 21
  • 31
  • you can do this using synchronization http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/ – Nas Nov 30 '15 at 18:58
  • I think server push notification is the best. Instead of pulling data every 5s, server will push GCM to client whenever new data is available. When GCM notification comes, client could start request for real data & update database or UI after received data. For service on Android, maybe you want to refer to this link https://www.youtube.com/watch?v=xHXn3Kg2IQE. Hope it's useful. – duynt Nov 30 '15 at 20:45

1 Answers1

1

Not sure if you really need to pull data every 5 seconds. Of course, it is too much. You have two options:

  1. Use GCM as @duynt suggested in comment. Follow Try cloud messaging for Android if you've never used it. This way you don't need to worry managing your service locally but whenever there is a latest data available, you will get notification so you can place request to get that and update in notification.
    GCM needs An application server that you must implement in your environment. This application server sends data to a client app via the chosen GCM connection server, using the appropriate XMPP or HTTP protocol. Take a quick look About GCM connection server.

  2. For any reason if you would like to pull data from your local Android Service component, you can still do that. But 5 seconds frequency is really high. As majority of the times the device is in sleep mode, you have to wake up the device then send request to pull data. Waking up device every 5 seconds means a battery drain along with consuming data.
    If you still want to proceed with local service option by increasing the frequency, make sure you follow How to use http in sleep mode and implement it that way otherwise it wont work in deep sleep mode.

You have to make a decision now.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
  • Thanks for a good answer! I see that pulling data from a local android service is bad choice. What about synchronization as @Nas suggested in comment? I see that many Android applications uses this approach too. – Uroš Podkrižnik Dec 01 '15 at 07:35
  • I think you can user that too. But find the differences and see what did to your requirement. Regarding sync adapters I see this note "Note: Sync adapters run asynchronously, so you should use them with the expectation that they transfer data regularly and efficiently, but not instantaneously. If you need to do real-time data transfer, you should do it in an AsyncTask or an IntentService.". So data sync may not happen at exact time. But that may be fine for your app. – cgr Dec 01 '15 at 08:54
  • so, what was the final decision ? and why ? – cgr Dec 02 '15 at 15:16