Reputation:
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!
Upvotes: 1
Views: 3944
Reputation: 23655
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.
Upvotes: 0
Reputation: 643
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.
Upvotes: 0
Reputation: 808
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.
Upvotes: 1