javaandroidperformanceintentservicenanohttpd

What is the best way keep your app updated with server?


My app consists of login based user system in which the user will receive his tasks. User's list of task need to be regularly synced with the server so that any new task would reflect immediately on the app. I have done this kind of sync stuff before in other apps but i would like to know the best approach to do it. I have two approaches in my mind please let me know which one is better or if you have any another idea then please tell me.

APPROACH 1

I can use IntentService and AlarmManager to to run background service at fixed interval of time and check for any change on sever through a http request (i.e. app to server request).

APPROACH 2

I can host a small TCP server through my app using NanoHTTPD library and after every new entry on server i can make request from the server to this small TCP host informing about these changes (i.e. server to app request).

I have used APPROACH1 before but i am interested in APPROACH2 more( i think this will drain less battery). So please let me know which one is better way(i.e. more efficient, secure, etc) to do it. If you have any other idea then please feel free to share it.


Solution

  • both your approaches will drain the battery dramatically.

    the best practice here is using push notification. all you have to do is registering you app with one of the well known providers of the service like FireBase Cloud Messaging and then configure your app's server to send notifications to the Cloud Messaging service which will handle the delivering of your notification.

    and you won't have to worry about battery usage because they use google play services to keep a connection open.

    what you have to do is to simply override the onMessageReceived method of the service and update your data from there.