1

I am working on receiving live API data updates on my android app. I am against polling as it drains battery and not in favor of anything that requires to check data updates manually every 5 mins or so. Is there an easier way to achieve this without the app constantly polling or manually checking if the data has updated on the API side?

I was thinking of adding a listener to some sort of an update on the server, but not sure if that'll be a viable approach. I have referred to : How to do live updates on an android app and other threads online, but nothing really helps. Here's an example of how I retrieve my data currently with observables:

 getCompositeDisposable().add(getDataManager()
                .getGuestListApiCall(AppPreferencesHelper.getInstance().getCurrentUserId())
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(response -> {

                  // using the parsed response here.
             }, throwable -> {
                    if (!isViewAttached()) {
                        return;
                    }

                    // handle the error here
                    if (throwable instanceof ANError) {
                        ANError anError = (ANError) throwable;
                        handleApiError(anError);
                    }
                }));

Any idea how to go about this? Any sample app would be helpful with some sort of a dummy api that can help see the live update result.

Thanks in advance!

  • It all depends on what you are trying to accomplish. Generally you will want to use Firebase / GCM to provide Push notifications; a tiny low power ping sent via Google services which alerts your application to wake up and make the call to the API for more information. – Matt Clark Feb 04 '20 at 01:25
  • I make a call to my api containing data about specific rooms. The room info will keep updating over the course of the hour, but I don't want to keep polling to see what the new data looks like. Will firebase help with this scenario? does it consume a lot of battery if I just do it while the app is in foreground and disable it from doing so while in background? –  Feb 04 '20 at 01:56
  • Firebase will help you if the data updates while the user has the app closed, and you want to notify them / act on this change. If you only need to show the data when the user has the app open, then it is perfectly acceptable to open a new connection and fetch the data every few seconds while visible. – Matt Clark Feb 04 '20 at 01:58
  • wouldnt that drain the battery if i keep updating or polling non stop? –  Feb 04 '20 at 01:59
  • Relative to the power required to keep the phone itself awake, the small amount of network activity will be negligible. Don't poll as often as you can, poll on a sensical schedule for your app. Do you need to fetch every 5 seconds? 30 seconds? 1 minutes? 5 minutes? – Matt Clark Feb 04 '20 at 02:00
  • i plan on fetching it dynamically, as soon as the api data update, i want to see if i can add a listener that will listen for the changes in the api data. is that possible? –  Feb 04 '20 at 02:14
  • 1
    Then you might just want to use WebSockets. Open the TCP connection when the user open the app or when the screen is turned on. It uses the HTTP protocol but with a KeepAlive, it allows the server to arbitrarily send data to the client. Or just open a TCP and send data your own way; again, the power required for your minimal network activity is fairly negligible vs the power required to keep the device awake and responsive. – Matt Clark Feb 04 '20 at 02:27
  • how do I do that? do you have a sample app I can see it working with? I have no clue how tcp even works –  Feb 05 '20 at 01:04
  • @AngelaHeely Found this sample project which uses LiveData with Websockets. https://github.com/McLeroy/WebsocketLiveDataSample – Badhrinath Canessane Feb 05 '20 at 09:58

3 Answers3

1

If i understood your problem, you want to get live data from API in real Time, or each 5 minutes, so to do it you have to use SOCKET.IO I hope that helps you.

Abderazak Amiar
  • 776
  • 7
  • 22
0

What I implemented in a chat application is when someone sends me message, I configured server in such a way that it sends me fcm pushnotification with chat id. When I receive the push notification, I call an API to get the details of the chat. This method may help you. If you need the code details, let me know.

Cecil Paul
  • 595
  • 6
  • 27
0

You should use push notifications. They are not only working when your app is in background or not running, but also when your app is open.

Using your own websocket connection is not recommended and also would not work when your app is closed.

The GCM system basically maintains a single websocket connection between the Android device and Google's GCM/Firebase servers. Since this is part of the Android system, it can wake up your app when a pushnotification arrives that is meant for it.

Ridcully
  • 23,362
  • 7
  • 71
  • 86