1

Using the Setexpedited() function in Workmanager ( 2.7 >), is useful to start a persistent work that last even if the user close the app and/or put it in background.

The problem is that it needs to show a notification everytime the work starts, even when the app is already active and in foreground, so a notification should not be necessary.

Is there a way to avoid workmanager/setexpedited to show the notification if the app is in foreground?

Bofaquien
  • 11
  • 1

1 Answers1

1

As per:

https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running

Under the hood, WorkManager manages and runs a foreground service on your behalf to execute the WorkRequest, while also showing a configurable notification.

The short answer is "NO". What you have in that case(what WorkManager is using under the hood) is a ForegroundService:

https://developer.android.com/guide/components/foreground-services

Here is a question about removing the notification of a ForegroundService:

How to startForeground() without showing notification?

From the top answer:

However, it is possible to have a "fake" notification, i.e., you can make a transparent notification icon (iirc). This is extremely disingenuous to your users, and you have no reason to do it, other than killing their battery and thus creating malware.

Yavor Mitev
  • 1,363
  • 1
  • 10
  • 22
  • 1
    I do not want to remove the notification from a foreground service. Consider an app with a simple button, when the user tap the button some "work" starts. I need this work to continue, if needed, even if the user then leave/close the app. The best way is to use an expedited work with WorkManager. I'm OK in showing the notification if the work re-start (retry), when the app is not in foreground anymore, but why I have to show a notification to the user everytime he taps the button? Is this avoidable? – Bofaquien Jun 29 '22 at 17:19
  • @Bofaquien because the WorkManager is using a ForegroundService – Yavor Mitev Jun 29 '22 at 17:27
  • Yep, that is clear, also a nonsense, when the user tap a button the app is in already running a foreground process, workmanager should use it, and run a different foreground service if the app is in background/closed, that is when the notification makes sense to alert the user that a sleeping app is doing something under the hood. Hope Google is going to correct this in a later feature. – Bofaquien Jun 29 '22 at 18:56