1

I am troubleshooting my Firebase Push Notifications Delivery issues and I keep getting people mentioning 'If app is in the Background.....'

One such example is in this SO question How to handle notification when app in background in Firebase

What does App in the background mean?. Is it when the App is closed/killed??

I notice there is also a concept of background service, which I'm confused if my app is even having one? I don't know maybe some libraries activate it.

In push notifications, my main concern is how to get the push notification delivered when the App is closed. Users are always in the habit off:

  • Swipe close all opened apps
  • Restarting device (typical when battery goes flat at night)
Omar Shawky
  • 1,242
  • 1
  • 12
  • 25
user1034912
  • 2,153
  • 7
  • 38
  • 60

1 Answers1

1

by natural in any smart phone you can have multiple apps running at once. When you have an app running, but it’s not the focus on the screen it is considered to be running in the background.

as for services there're 3 types of them (quoting from android developer documentations) :

Foreground

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.

Background

A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

anyway, background services now have certain limits starting with android 8 (api 26) and it's recommended to use long-running-workers through WorkManager

Bound

A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

As for if your application having a service or not, if you didn't include/define one in the app manifest, then no your app doesn't have one by default as far as I know.

You can read more about services from this link

I don't know much about firebase, I just wanted to help and clarify some of your questions.

Omar Shawky
  • 1,242
  • 1
  • 12
  • 25
  • 1
    Thank you Omar, so when the app is swipe closed it is not considered background? – user1034912 Sep 11 '21 at 02:25
  • 1
    yes I guess so, it's only in background if you can switch back to it using home button. – Omar Shawky Sep 11 '21 at 02:26
  • 1
    but you can still make a service or a worker run in the background with the user knowing (using a status bar notification marking the service as a foreground one which would make it live longer than a background one) or without the user knowing to do essential/necessary work under certain limitations imposed by the system, like if the system is running low on memory, it could terminate your service entirely and run it back later when there's memory. – Omar Shawky Sep 11 '21 at 02:30
  • 1
    @user1034912 also regarding **Restarting device (typical when battery goes flat at night)**, you can make a service to start on BOOT_COMPLETED signal (which is broadcasted once when your device turns on after a shutdown/restart), you need to assign a broadcast receiver to wait for this signal, when broadcast receiver receives it, you start a foreground service to do whatever you want using the database. – Omar Shawky Sep 11 '21 at 02:53
  • 1
    you can read about broadcast receiver here - https://developer.android.com/guide/components/broadcasts?hl=en#receiving-broadcasts – Omar Shawky Sep 11 '21 at 02:55
  • 1
    So the background service runs even if the App is Swipe Closed (Or simply closed)? – user1034912 Sep 11 '21 at 02:58
  • 1
    as I mentioned before using a 'background service' has gone complicated since android 8 (api 26), but if you're willing to show a status bar notification while your service is working in the background in that case it's called 'foreground service', which yes could run while the app is closed as long as it wants (as far as I know, I didn't test its limits), you can start reading its documentation see if it fits your criteria - https://developer.android.com/guide/components/services – Omar Shawky Sep 11 '21 at 03:04
  • 1
    if you're so interested in its limits, you can write a new question see what people say, they would know better than me indeed. just differentiate between 'background' and 'foreground' as its very important due to their differences and limitations. – Omar Shawky Sep 11 '21 at 03:09
  • 1
    apparently there's actually some limitation for making a service keep working if the app was already open and the user closed it which in this case would kill the service, but this according to this question's answer could be avoided by making the service send a new signal to restart it again just before it gets destroyed - https://stackoverflow.com/questions/30525784/android-keep-service-running-when-app-is-killed – Omar Shawky Sep 11 '21 at 03:31
  • 1
    in this case above, I can't actually confirm or deny that this way would make your service run 24/7 on the mobile but if it worked would be somehow abusive to the user's mobile resources and should be avoided. – Omar Shawky Sep 11 '21 at 03:32