My application is quite simple: it downloads a JPG from a static URL with Picasso then displays it. However the picture behind the URL is regularly updated without changing the URL itself. When this happens, the application has to show a notification about it, even if the application haven't been started since boot.
My question is: what is the recommended and modern way to do it?
My biggest problem is that a lot of tutorials and documentation contain 3+ years old information and it's quite confusing which is the right for me without it being outdated.
Currently my concept is having a BroadcastReceiver which is subscribed to a boot event. This starts a JobService with an Intent, and the onStartJob method displays the notification. Am I right here or am I missing something?
EDIT:
Thanks to CommonsWare for his comment, WorkManager is the way to go.
I've written my own Worker class and tried to enqueue a Work in the MainActivity's onCreate method:
if (firstRun) {
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresStorageNotLow(true)
.build();
PeriodicWorkRequest updateRequest = new PeriodicWorkRequest.Builder(NewPaperWorker.class, 30, TimeUnit.MINUTES)
.setConstraints(constraints)
.addTag("update")
.build();
// Enqueue the work
WorkManager
.getInstance(this.getApplicationContext())
.enqueue(updateRequest);
// After starting the work, the firstRun variable can be set to false
final SharedPreferences preferences = this.getSharedPreferences("PREFERENCE", MODE_PRIVATE);
preferences.edit()
.putBoolean("firstRun", false)
.apply();
}
The Worker shows a notification, that's its only job. It indeed sends a notification for the first time, but after that, despite of the PeriodicWorkRequest, it seems to stop completely. According to the developers' guide it should be showing notifications regularly about every 30 minutes. What am I missing?
I have a Xiaomi Redmi Note 5 and enabled Autostart for the app.