1

How do you make notifications keep showing up while an app is closed? I have code kind of like this:

DatabaseReference ref = database.child(username);

ref.addValueEventListener(new ValueEventListener() {
//some stuff here
}

Here, I give the user notifications whenever they get a new message or something, which works fine while they have the app open. How do I make this listener keep listening and notifying, even after the user closes the app? I've read online about making a Service or Intent yada blada, but none of that seems to work with Android 8+... any advice? I also read something about making a foreground service, but the app crashed when I tried that because I don't know anything about those.

iknow
  • 4,796
  • 9
  • 19
  • 37

1 Answers1

0

You should use a Foreground Service or WorkManager, which is part of Android Jetpack. WorkManager from the docs:

WorkManager is an API that makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts. The WorkManager API is a suitable and recommended replacement for all previous Android background scheduling APIs, including FirebaseJobDispatcher, GcmNetworkManager, and Job Scheduler. WorkManager incorporates the features of its predecessors in a modern, consistent API that works back to API level 14 while also being conscious of battery life.

Check out more at the: official doc

If you are working with firebase, you should check out FCM too, at firebase's official doc