invertase / notifee

βš›οΈ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.8k stars 213 forks source link

Android: startForegroundService() not allowed due to mAllowStartForeground false #470

Open rcidt opened 2 years ago

rcidt commented 2 years ago

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.

mikehardy commented 2 years ago

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)

rcidt commented 2 years ago

@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.

mikehardy commented 2 years ago

: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

rcidt commented 2 years ago

@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.

mikehardy commented 2 years ago

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

frozencap commented 2 years ago

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

MEsteves22 commented 1 year ago

@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.

theBlbDan commented 1 year ago

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

github-actions[bot] commented 1 year ago

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.

zoobibackups commented 1 year ago

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 ?anon0 (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)

bfelbo commented 9 months ago

@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?

mikehardy commented 9 months ago

@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

bfelbo commented 9 months ago

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 πŸ™

github-actions[bot] commented 8 months ago

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.

bfelbo commented 8 months ago

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

NadeemKhanFh commented 7 months ago

Yes it still needs to be fixed.

github-actions[bot] commented 6 months ago

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.

bfelbo commented 6 months ago

It's not been fixed and still requires attention

abdoutech19 commented 5 months ago

Still not fixed

github-actions[bot] commented 4 months ago

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.

bfelbo commented 4 months ago

It's not been fixed and still requires attention

mihaibulic2 commented 4 months ago

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.

github-actions[bot] commented 3 months ago

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.

bfelbo commented 3 months ago

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

IskanderNazarov commented 3 months ago

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

anujmv commented 3 months ago

yes

github-actions[bot] commented 2 months ago

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.

bfelbo commented 2 months ago

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

github-actions[bot] commented 1 month ago

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.

bfelbo commented 1 month ago

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

rcidt commented 1 month ago

I concur with @bfelbo

github-actions[bot] commented 2 weeks ago

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.

GeorgesMainville commented 2 weeks ago

Still not stale