notifee icon indicating copy to clipboard operation
notifee copied to clipboard

Android: startForegroundService() not allowed due to mAllowStartForeground false

Open rcidt opened this issue 2 years ago • 36 comments

Everything works fine when the app activity is running (in foreground or background).

But when the app is killed, the app starts in headless mode and then fires the notification as a Foreground Service. In this scenario, the notification does not show and notifee throws this error:

Error: startForegroundService() not allowed due to mAllowStartForeground false: service app.notifee.core.ForegroundService
    at Object.promiseMethodWrapper [as displayNotification] (NativeModules.js:106:51)
    at NotifeeApiModule._this.displayNotification (NotifeeApiModule.js:265:32)

If I set asForegroundService to false, the notification will show but without any styling.

I am on latest notifee version 5.4.1.

This is definitely related to Android 12 and the new Foreground Service restrictions imposed on apps, but according to this, my case should be exempt from these restrictions, since my app meets this exception Your app receives a high priority message using FCM.

rcidt avatar Jul 19 '22 13:07 rcidt

Hmm - perhaps our code here has an incorrect check or similar? Have you looked in to the code here to see where mAllowStartForeground is defined + used to see why it's behavior is not matching expectations?

Could be in either https://github.com/invertase/notifee/tree/main/android or https://github.com/invertase/notifee/tree/main/packages/react-native/android depending on what level it's at (core lib or react-native adapter)

mikehardy avatar Jul 19 '22 15:07 mikehardy

@mikehardy A quick search using the github search feature yields 0 results. I will try another search on a local copy of this repo later, but I don't think this error is generated by the library. I believe this is originating from the OS.

rcidt avatar Jul 19 '22 15:07 rcidt

:thinking: confirmation bias but AOSP uses hungarian notation and the variable name 'mAllowStartForeground' is Hungarian notation so could be AOSP. I don't think we use that style here.

This may not be something we control. I think Android 12 can downgrade your priority if too many notifications come in for your app's energy bucket ? Pretty vague guess though

mikehardy avatar Jul 19 '22 16:07 mikehardy

@mikehardy no results show up while searching local copy.

Unfortunately, I don't think this has to do with the priority of the push notification, since it happens 100% of the time on fresh installs. For now, downgrading the target sdk to 30 seems like our only (temporary) solution.

Does notifee have an Android api? I'd like to try launching the notification on the native side.

rcidt avatar Jul 19 '22 16:07 rcidt

Any java code imported in the app's symbol space is available as an API, you can just call things I think

mikehardy avatar Jul 19 '22 16:07 mikehardy

Apps that target Android 12 (API level 31) or higher can't start foreground services while running in the background, except for a few special cases. If an app tries to start a foreground service while the app is running in the background, and the foreground service doesn't satisfy one of the exceptional cases, the system throws a ForegroundServiceStartNotAllowedException.

https://stackoverflow.com/a/70666991 https://developer.android.com/guide/components/foreground-services#background-start-restrictions

frozencap avatar Aug 24 '22 19:08 frozencap

@rcidt were you able to find a solution? I'm also having the same problem even though I'm using a high-priority notification. I've been trying to find a solution but to no avail.

MEsteves22 avatar Oct 27 '22 17:10 MEsteves22

This is a possible bug in AOS 12 & 13, or like me, if the app happens to go background while the service is starting ( Executing stopForeground() or startForegroundService() ) it will drop this error and crash the process. Bug: Fatal Exception: android.app.ForegroundServiceStartNotAllowedException

theBlbDan avatar Nov 13 '22 17:11 theBlbDan

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar Dec 11 '22 18:12 github-actions[bot]

Possible Unhandled Promise Rejection (id: 0): Error: startForegroundService() not allowed due to mAllowStartForeground false: service com.mobile.number.locator.phone.caller.location.finder/app.notifee.core.ForegroundService Error: startForegroundService() not allowed due to mAllowStartForeground false: service com.mobile.number.locator.phone.caller.location.finder/app.notifee.core.ForegroundService at promiseMethodWrapper (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.mobile.number.locator.phone.caller.location.finder&modulesOnly=false&runModule=true:20174:45) at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.mobile.number.locator.phone.caller.location.finder&modulesOnly=false&runModule=true:1638:50) at ?anon_0_ (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.mobile.number.locator.phone.caller.location.finder&modulesOnly=false&runModule=true:234381:55) at next (native) at asyncGeneratorStep (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.mobile.number.locator.phone.caller.location.finder&modulesOnly=false&runModule=true:1306:26) at _next (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.mobile.number.locator.phone.caller.location.finder&modulesOnly=false&runModule=true:1325:29)

zoobibackups avatar Jul 11 '23 09:07 zoobibackups

@originalmoose I see that you've forked Notifee and changed the foreground service. Were those changes to fix this error and did you fix it?

bfelbo avatar Dec 11 '23 08:12 bfelbo

@bfelbo indeed - I just went over there to see what the diff looked like and it is a little impenetrable because of this repos main module / git submodule setup 😬 https://github.com/invertase/notifee/compare/main...trainerroad:notifee:main

I'm not sure what the changes from that are so I'm not sure I could cleanly carry them over here as a PR

Either way this appears to be a real issue and should not be closed

mikehardy avatar Dec 11 '23 14:12 mikehardy

Awesome, thanks for reopening!

Either way this appears to be a real issue and should not be closed

Yes, indeed. This is our biggest Android crash by far, causing thousands of crashes pr. month. It would be amazing to fix it 🙏

bfelbo avatar Dec 11 '23 14:12 bfelbo

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar Jan 08 '24 15:01 github-actions[bot]

It's not been fixed and still requires attention, cc @mikehardy

bfelbo avatar Jan 08 '24 15:01 bfelbo

Yes it still needs to be fixed.

NadeemKhanFh avatar Jan 29 '24 20:01 NadeemKhanFh

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar Feb 26 '24 20:02 github-actions[bot]

It's not been fixed and still requires attention

bfelbo avatar Feb 27 '24 08:02 bfelbo

Still not fixed

abdoutech19 avatar Mar 19 '24 17:03 abdoutech19

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar Apr 16 '24 17:04 github-actions[bot]

It's not been fixed and still requires attention

bfelbo avatar Apr 17 '24 10:04 bfelbo

This is also our biggest crash by far. It’s been 4 months since the last real interaction. Can we get a straight answer: is this going to be fixed soon or are we going to do the bot-closes-someone-comments-not-fixed dance forever?

not trying to be an ass just want a straight answer to know what I can expect.

mihaibulic2 avatar Apr 17 '24 17:04 mihaibulic2

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar May 15 '24 17:05 github-actions[bot]

Still not stale. It's by far our biggest crash.

bfelbo avatar May 16 '24 10:05 bfelbo

I'm also getting a lot of errors of this type

IskanderNazarov avatar May 20 '24 08:05 IskanderNazarov

yes

anujmv avatar May 28 '24 06:05 anujmv

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar Jun 25 '24 06:06 github-actions[bot]

Still not stale. It's by far our biggest crash.

bfelbo avatar Jun 25 '24 08:06 bfelbo

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

github-actions[bot] avatar Jul 23 '24 09:07 github-actions[bot]

Still not stale. It's by far our biggest crash.

bfelbo avatar Jul 23 '24 11:07 bfelbo