Search code examples
javaandroidandroid-notificationsandroid-workmanager

How to check for updates when the app is closed?


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.


Solution

  • The WorkManager is the solution for the original question, it's quite simple and straightforward to use, thnak you CommonsWare!
    After testing, I found the solution for my second question as well. The code above is absolutely fine, didn't need to modify it by a single character. The real problem was the Chinese phone, I had to turn off battery restrictions for my app to make the code work fine.
    On a Xiaomi Redmi Note 5 it was
    Battery & performance -> Battery entry/Settings icon -> App battery saver -> search for app -> No restrictions
    If it doesn't work for you, maybe
    Apps -> Permissions -> Autostart -> Turn on will solve this issue.
    Additional info about this can be found here and here.