3

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.

Piyush Kumar
  • 623
  • 7
  • 11

2 Answers2

5

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.

AndroidSmoker74
  • 288
  • 4
  • 19
  • 1
    Thanks for quick response. I didn't knew that google play services keeps the connection open this was really helpful for me. – Piyush Kumar Nov 28 '17 at 17:17
2

Why don't you try to use the push notifications service? It would be a better option in terms of battery life and network use.

EDIT: In my opinion would be better avoid pulling (requesting to the server periodically) in order to save battery and economize the use of the network. So the solution could be implement push notifications (your server will notify the app that there is something new to request) through a service like Google Cloud Messaging/Firebase.

anj0
  • 44
  • 3