I am working on an android app. In which, I want to automate Notification. The way I am thinking is first,
Fetch data from the server, then
Store in Room DataBase, then
Get data from RoomDb and then Display Notification.
Repeat this every day at least once.
Now, I want this work done in the background under any condition. That is, whether the app in the background or whether the app is closed or whether the phone is restart.
So in any situation, Fetch data→Store it→Display Notification.
I found many android background processing libraries. Such as AlarmManager, JobScheduler, BroadcastReceiver, JobIntentService, Firebase Job Dispatcher, WorkManager, etc. I am working on API 19 to API 28 or higher. These libraries have limitations and having challenges in background processing.
So, which library will be suitable for me to build an Automate Notification which works well on API 19 to API 28 or higher.
You can use Jetpack WorkManager. It is backwards compatible up to API 14.
WorkManager internally uses JobScheduler on devices with API 23+
and a combination of BroadcastReceiver + AlarmManager on devices with API 14-22.
Ensures task execution, even if the app or device restarts.
Related
I'm software engineer using Flutter in Japan.
Developing an alarm app that wakes me up in the morning, and having trouble implementing the following.
I would appreciate it if you could help me.
Question.
I would like to know if there is a way to execute a specific process at a scheduled time,
even if the app is killed or backGround.
Details.
I'm currently developing on an alarm app that sends local notifications & custom music at a specified time.
I was able to schedule local notifications to send at the specified time.
However, I can't find a way to run a specific process in the background at the same time.
(It's an image of a process that plays specific music selected by the user)
What I've found out
Confirmed that notifications can be sent on a specified day and at a specified time every week using flutter_local_notifications.
(execute flutterLocalNotificationsPlugin#zonedSchedule())
However, in flutter_local_notifications, there is no such thing as a listener to receive the notification itself.
(There is an onSelectNotification, but it doesn't work until user taps the notification bar.)
The library author says that there is no listener to fire such notification timing,
So I decided to use only local notifications in this library and look for another library to replace the background processing.
https://github.com/MaikuB/flutter_local_notifications/issues/1201
I found background_fetch and WorkManager to be particularly useful, but I gave up on using these libraries as well.
The reason is that with the above two libraries can only be executed in the sense of 15 minutes, as shown below, due to iOS limitations,
and I suspect that the process cannot be executed at the intended timing.
workManagaer
https://pub.dev/packages/workmanager#customisation-android-only
// Periodic task registration
Workmanager().registerPeriodicTask(
"2",
"simplePeriodicTask",
// When no frequency is provided the default 15 minutes is set.
// Minimum frequency is 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency.
If you have configured a lower frequency. frequency: Duration(hours: 1),
)
background_fetch
Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs.
I think the only way to do this is to use MethodChannel to write and execute native code for each OS from Flutter.
I would like to know if there is a way to execute the scheduled process at the specified time even if the app is in kill or background state in Flutter, even if it uses MethodChannel.
Sorry Its so long. Please let me know.
Thank you very much
The only doc provided for background process (alarm being one of the example cited) points out to an article on medium:
https://docs.flutter.dev/development/packages-and-plugins/background-processes
In my Android application the user has an option of using the application in offline mode. When the user opts to enter the offline mode, I download all the content from the server(which might take even upto 7 minutes) for offline usage. The usage of the application henceforth is dependent on the download of offline content.
I am using a service to download the offline content. But the service may not work in Android 8 if the app goes to the background. So what is the best approach to download the offline content for Android 8? Is it a foreground service or JobIntentService or a WorkManager?
Anything that is backed by JobScheduler — which includes JobIntentService and WorkManager — has a 10-minute limit. You indicate that your work may take up to 7 minutes, which makes me somewhat nervous.
In the short term, make your existing service be a foreground service, as that will keep your code working (other than any problems that Doze mode might impose).
If your 7-minute download work is really a series of smaller things that add up to 7 minutes, you might eventually migrate to WorkManager. Divide your work into smaller chunks and set up chained work with WorkManager, so you are certain to not go over the 10-minute limit for any of those chunks of work. Plus, WorkManager lets you establish constraints to say that your work should only be performed if you have an Internet connection. Right now (late August 2018), though, WorkManager is only 1.0.0-alpha07, so I would not ship a product based on WorkManager until it at least reaches a 1.0.0 final version.
The best approach would be to use WorkManager. As stated in the docs that:
WorkManager is intended for tasks that require a guarantee that the
system will run them even if the app exits, like uploading app data to
a server, or downloading data from server.
The benefits of using WorkManager over services includes handling of doze, standby, battery optimizations and constraint execution etc.
You can schedule a worker with WorkManager to download data for your app from server, and once data is available, you can go on with your offline mode.
We notice that AlarmManagerCompat alone, isn't a reliable way to implement alarm/ reminder feature in our app, due to different AlarmManager behaviour in different version of OS. (For instance, Doze mode)
Initially, we plan to use Evernote's android-job library, to help us implement alarm/ reminder feature in our app.
However, along the way, we also notice that Google just release WorkerManager.
So far, WorkerManager works well for us, when we run some one-time background jobs (Almost immediate, with internet connectivity constraint) after the app quit.
We have plan to use WorkerManager to implement alarm/ reminder feature.
I was wondering, how reliable is WorkerManager to implement such feature? Has anyone try it out? We are targeting API 15 and above.
WorkManager is not appropriate for anything that must fire at a specific time as jobs, including those used by WorkManager or android-job, will not fire while the device is dozing.
For exact timing, you should absolutely be using AlarmManagerCompat and specifically, setExactAndAllowWhileIdle() which fires an alarm at exactly the specified time on all API levels.
As your exact timed alarm can and will happen while the device is dozing, your app should not require network connectivity to post your alarm/reminder notification. Ideally, the information should be in the PendingIntent itself and not even need any database fetch/etc.
What is JobScheduler ? which type of android apps using this JobScheduler and why ? Please feedback with real example so that I could understand.
Here is a short summary:
1) AlarmManager.- Use it to post a notification or set off an alarm at a very specific time. Use this for executions that do not depend on condition.
2) JobScheduler.- Allows you to have execute jobs based on conditions. This is recommended if your app targets API>21.
3) JobDispatcher.- Similar behavior as JobScheduler, used as a JobScheduler-compatibility layer if your app targets versions lower than API<21. Note that this needs internet for real time execution, any task not executed because of internet not being available will be executed when internet becomes available.
I want to track users in specific period and times of days. I start alarm manager in every 2 minutes (i get periodic time from server and this is dynamic in my application) and try to get location about one minute, after that i stopped getting location and i save location taken on DB. anyway, this approach run very well but in api level 23 and above that, my application for doze and standby mode cannot run well and not call alarms in specific time! I used setExactAndAllowWhileIdle method for api level 23 and above that but not worked well.
The documents said, i can't use more alarms in doze and standby mode.
My question is, how can track user in android 6 and above?
While working with alarm in doze, use setAlarmClock() instead of set() as it is specifically designed for AlarmClocks, so it can run even in doze as well.
Alternatively,
Have you ever heard of Firebase-job-dispatcher ?
Firebase JobDispatcher is an open-source library that provides an API
similar to JobScheduler in the Android platform. Firebase
JobDispatcher serves as a JobScheduler-compatibility layer for apps
targeting versions of Android lower than 5.0 (API level 21).
Firebase JobDispatcher supports the use of Google Play services as an
implementation for dispatching (running) jobs, but the library also
allows you to define and use other implementations: For example, you
might decide to use JobScheduler or write your own, custom code.
Because of this versatility, we recommend that you use this Firebase
JobDispatcher if your app targets a version of Android lower than 5.0
(API level 21).
You should go for it. They have got pretty good configuration techniques as well.