Android migrate background service to jobscheduler - android

we have a project which uses background service to retrieve real-time events from server. we use s websocket connection to retrieve data. but now in Android Oreo or higher, os starts to showing battery Warning.
my concern is that if we migrate to jobscheduler does it stop after sometime. we want to show a notification if event happen
i tried job scheduler and it offers recurring invokes. but our connection is real-time so if jobscheduler stops we don't get data.
so how other apps handle this (example WhatsApp )
what is the best way to keep our connection live and retrieve data ?
thanks advance

my concern is that if we migrate to jobscheduler does it stop after
sometime.
Yes. During doze mode all the scheduled jobs are deferred until maintenance window is made available by the OS.
what is the best way to keep our connection live and retrieve data ?
You shouldn't maintain a persistent connection with your server and permanently run background service. Starting from Android O you won't be able to run Services in background. OS will terminate all the services and any attempts to start service in background will result in IllegalStateException
Your requirement seems that you need to use FCM. The basic idea is, instead of constantly asking server for data, let server notify the app about data availability. For more details and implementation steps, refer to official document.

Related

Invoke a custom code on internet connectivity in Android

I want to get latest updates from my server when I turn on the internet and generate a notification, but when the app is closed/killed/swiped from recent items, there is no way to keep my service alive and listening to network change event so that I can ping my server, I'm not sure how to do it and how other apps such as whatsapp does it when we receive new notification the moment we turn on mobile internet.
Any help is appreciated, thanks in advance.
Making clear what #GobuCSG commented. You have the following options:
Run your application all the time via a foreground service and listen for wifi connected broadcast. This is not a good solution and wastes battery life.
Schedule jobs using the WorkManager API.
The WorkManager API was developed specifically for the purpose you specified. The API allows you to set required conditions for your job to run, such as network connectivity. It also persists scheduled jobs through device reboots, so you don't have to. The only downside is that you don't have fine tune control of when the code runs, as one of the goals of WorkManager is to save battery life by delaying and batching jobs.
EDIT:
Another option is to use push notifications. This is useful if your are developing a messaging app and you want your server to push a message to the client so they can be notified of a received message. But, if all you need to do is establish contact with your server once a day, then you should use WorkManager.

Running a background service to fetch data from firebase database and performing certain action if particular value is received in the UI [duplicate]

I've just been reading about adding a service to my application, and since 7/8 there are now service restrictions to improve phone performance.
I've seen that the recommended approach is to use a job scheduler, but will that not just periodically start a new listener if I did that?
Basically I update my database, and using a snapshot listener I want to update my user in real time. When the app is closed, I'd like to send a notification.
My issues (if I'm correct) are that constantly making a new Firestore request will eat through my request allowance.
Also, if its a job scheduler it won't quite be real time?
I've read that you can use a foreground service, but this doesn't really seem like that task that needs a permanent notification and would annoy the user.
Anyone got any tips on how I'd implement this?
Thanks
Using a listener after an android application is closed
You can use a listener after an android application is closed, by not removing it. Once you are using a listener, you also need to remove it according to the life-cycle of your activity. But this will work only for a shot period of time because Android will stop your service if the app is not in the foreground. It does this to save resources when the app isn't being used. It also might stop your app from doing any networking, or even kill the app process completely. There's nothing you can do to prevent this, other than making it a foreground service, as you already mentioned.
A foreground service is probably not the best thing to do for your case, nor is it the best thing for your users. Read more about limitations on background services.
My recommendation is to use Firebase Cloud Messaging to notify your app when something has changed that it might be interested in. So your users will recieve notification even if they will keep their app closed.

App monitor location on background service

I would like to make an app that always works in the background (from boot up), which sends GPS coordinates to a server. This app should ALWAYS be active and should never close.
Should I use the services? I would like to use UDP sockets to send coordinates but I accept alternatives. I would also like to avoid using the google API.
Thanks a lot :)
If your app need to run in the background , you need Service and you need to make it a foreground service which means you need to show a notification to the user as long as your application is running.
To open app on device boot, from Android O, its not allowed. You will get an IllegalStateException.
The main reason for this is to prevent exactly what you are trying to achieve.
Its not good to keep running an app in the background and its especially bad to keep tracking users GPS coordinates and send it to the server.
Because it will drain the battery very soon.
However it is possible to keep a foreground service running which can take the GPS coordinates and send it to the server. But for that user has to open your App first.
Please refer to
https://developer.android.com/about/versions/oreo/background#services
Other alternative is to use JobIntentService which will schedule your tasks in smart ways to avoid draining users battery and data.
Regarding UDP sockets , it depends on your backend.

How to get data from the server when the Android app is closed when using the service and display in the notification

How to get data from the server when the Android app is closed when using the service and display in the notification
Example :
I'll design a program that communicates with the server every few seconds and displays the values received in the notification. Even when the program is closed or when the phone is turned on, it still has the information exchange server.
You can run a service using startForeground to prevent it from being killed.
However
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory.
But still if you just want to send a notification you can integrate firebase in your server to show the notifications.
Notifications can even be shown when the app is not running in the background.
Setting up firebase in your android application:
https://firebase.google.com/docs/cloud-messaging/android/client
Since the inception of Android Oreo, services are killed as soon as Application gets in background state. You could use Foreground Service to avoid getting killed but that will display a persistent notification to the user.
Going by your needs, I would suggest you to use WorkerManager. It will choose best JobScheduler for you, and can perform task repeatedly as per your needs.

Best Practice for network operation at regular intervals?

I am making an application which contains performing network calls to look for updates. Can anyone help with some best practices to perform this task. I can make a service to run on background and perform network operation using Handler at regular intervals but this would consume a lot of data and battery. Is there any other way to do this?
Use an AlarmManager or JobScheduler (depending on API level), and pick a sane frequency. Doze mode will stop you from going too insane.
You could work with Push Notifications from any server when the backend recognizes an update. It's better for the server itself to listen to updates at a regular interval of time rather than the app, because of the reasons you listed.
When the app receives a push notification, it would mean that it needs updating.
There are a couple of options for you:
Azure Notifications Hub: https://learn.microsoft.com/en-us/azure/notification-hubs/
Firebase Cloud Messaging: https://firebase.google.com/docs/cloud-messaging/

Categories

Resources