-
Notifications
You must be signed in to change notification settings - Fork 239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android: onForegroundEvent not listening to onPress Event #621
Comments
@helenaford As requested by you, created this issue |
Thanks for posting this. What I did:
What I get:
What I dont get:
Opening the app if not in foreground works, I just dont get notified about the event.
expo config
|
Hey @qroac, while this is getting fixed, I've just done a temporary workaround. Assuming that, you are using redux-toolkit and react navigation. Here are the steps, that I took..
Here are the code snippets
|
I'm also facing the same problem, onForegroundEvent not getting fired when pressing on the notification on Android |
Me too. Having same problem. Can't receive foreground event
|
can you post a step-by-step of how to recreate this bug? it's not clear to me as you mention both the background and foreground event handlers. This is the payload from our test app:
|
ohhh, isn't this the same issue #529? I think I understand now what you are saying the problem is... that you're receiving a background event but you think it should be a foreground event instead 🤔 |
This is a huge problem for us. It seems to me that there is not a thorough testing process for releases. I have the following testing process for both iOS and Android which would be good to mirror at some level. I get the desire for automated testing, but every release I run into an issue with FCM + Notifee.
|
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?
Thank you for your contributions. |
if you use |
The issue is still present in 7.4.0 |
Still having this issue in 7.5.0 I could make a workaround using redux and redux persist, following the example posted by quicksilverr. Thanks |
still having this issue, i can listen DELIVERED event, but when i press the notification it does nothing |
Having the same problem, only receive delivered events onForegroundEvent but no press events. |
I also checked that it was not working. Event 1 is not displayed after clicking Alert. on Android |
Same issue here! Android 12. Only "delivered" event works. Issue still present in 7.6.1 |
does anyone know when this stopped working? I'm looking into this now, can see the issue you describe. definitely hasn't always been like this. i wonder if it's todo with android 12 +. |
It's been an issue for at least the last 5 months. Didn't seem to be an issue in October last year but by December I've been fighting it. |
In my case, there was a problem with Android 12 version. |
Hey All, I've pushed a solution and a pre-release of v7.7.0-0.
I've left some notes on the p/r with a screen recording showing the final result. #720 I will not actually publish this as a final version until Monday which gives time for feedback. Reopened issue for now until it's in a final release. |
Hey @helenaford I am currently facing with triggering the onPress event of notification click when a notification is received in the background. As when a notification is received in the foreground, the onPress event is triggered without any issues. However, when the notification is received in the background, the onPress event is not triggered (notifee.onBackgroundEvent). I have added my index.js class code
|
me too. @Sandeep145XamRN
I tested it with v7.7.0-0. The devices tested are Galaxy s20 and Android 12 versions. @helenaford Thank you for your efforts to solve this problem. |
I'm facing the same issue @Sandeep145XamRN @k0502s have you found any solution? "react-native": "0.70.6", "@notifee/react-native": "7.7.0-0"@helenaford thanks for a great library. I understand that you are dealing with a lot of issues, but I wanted to bring your attention to a particular issue, which is crucial, I'm unable to work on notification re-direction. Any guidance will be appreciated. |
When using remote notifications on React Native, I've had the most luck with these docs. It directly states that onForegroundEvent will fire when it's a remote notification. I'm able to send notifications via the Firebase Messaging Console. The way I've handled it is to place the notifee.onForegroundEvent subscriber onto my home screen and then handle any navigation logic from there. That way I have access to a navigation object and can navigate or pass params. Surprisingly, this onForegroundEvent code even runs from the kill state. I am only able to get onBackgroundEvent to fire from local notifications while the app is running, not remote notifications Using // One Time USE EFFECT function to set things up!!
useEffect(() => {
const unsubscribeNotifee = notifee.onForegroundEvent(
async ({ type, detail }) => {
console.log(
`The type is`,
type,
"the detail is",
detail
);
switch (type) {
case EventType.PRESS:
if (detail.notification?.remote) {
console.log("It was a remote notification")
// Do some navigation logic
}
break;
case EventType.ACTION_PRESS:
console.log(`It was an ACTION PRESS THO`)
break;
}
}
);
return () => {
unsubscribeNotifee();
};
}, []); |
If you're using RN >= 0.73 (Bridgeless Enabled) latest notifee and above Android 12, onForegroundEvent and onBackgroundEvent event are not working. I think I found a way. the main reason notifee event not working is, ReactContext is null. due to new architecture system, getting react context dynamically is not working as expected.
private static @Nullable ReactContext getReactContext() {
ReactNativeHost reactNativeHost =
((ReactApplication) EventSubscriber.getContext()).getReactNativeHost();
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
return reactInstanceManager.getCurrentReactContext(); // <- returns null
} since mCurrentReactContext is null, sendEvent can't do the job, just end function to prevent further error. static void sendEvent(WritableMap eventMap) {
try {
ReactContext reactContext = getReactContext();
if (reactContext == null || !reactContext.hasActiveCatalystInstance()) {
return;
}
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(NotifeeEventSubscriber.NOTIFICATION_EVENT_KEY, eventMap);
} catch (Exception e) {
Log.e("SEND_EVENT", "", e);
}
} fortunately, there is a dirty & quick fix. after reading RN docs (https://reactnative.dev/docs/native-modules-android#sending-events-to-javascript), something came to my mind. notifee initializes modules with NotifeeApiModule, you can get reactApplicationContext from NotifeeApiModule.
public class NotifeeApiModule extends ReactContextBaseJavaModule implements PermissionListener {
private static final int NOTIFICATION_TYPE_DISPLAYED = 1;
private static final int NOTIFICATION_TYPE_TRIGGER = 2;
private static final int NOTIFICATION_TYPE_ALL = 0;
private static ReactApplicationContext context; // <- add this line
public NotifeeApiModule(@NonNull ReactApplicationContext reactContext) {
super(reactContext);
context = reactContext; // <- add this line
}
// add this function to access react context anywhere
public static ReactApplicationContext getContext() {
return context;
} and edit private static @Nullable ReactContext getReactContext() {
// ReactNativeHost reactNativeHost =
// ((ReactApplication) EventSubscriber.getContext()).getReactNativeHost();
// ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
// return reactInstanceManager.getCurrentReactContext();
return NotifeeApiModule.getContext(); // <- add this line and import
} yes. works well. no more problem with onForegroundEvent and onBackgroundEvent. but this fix cause another problem: If you press notification from background, bundle loads again and cause weird error.
++ If you're not interested in fixing ApiModule, there is other way to do it. but this one won't fix onBackgroundEvent. private static @Nullable ReactContext getReactContext() {
if (DefaultNewArchitectureEntryPoint.getBridgelessEnabled()) {
ReactHost reactHost =
((ReactApplication) EventSubscriber.getContext()).getReactHost();
return reactHost.getCurrentReactContext();
} else {
ReactNativeHost reactNativeHost =
((ReactApplication) EventSubscriber.getContext()).getReactNativeHost();
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
return reactInstanceManager.getCurrentReactContext();
}
} |
After many research, pressing notification from background -> opens App -> isAppInForeground() returns false -> run HeadLessJsTask -> error! what's going on here? pressing notification will make app pops up (bringing app status background to foreground), so it should be foreground event. (well still, background event is okay to me. if it's working correctly) this is confusing. even if app is background, it's still alive, should be return 'RESUMED' event... but It's not. maybe react context lifecycle detects status better than ProcessLifecycleOwner? let me check... static boolean isAppInForeground() {
Log.d("Notifee - ProcessLifecycleOwner", ProcessLifecycleOwner.get().getLifecycle().getCurrentState().toString());
Log.d("Notifee - ReactContext Lifecycle", NotifeeApiModule.getContext().getLifecycleState().toString());
return ProcessLifecycleOwner.get()
.getLifecycle()
.getCurrentState()
.isAtLeast(Lifecycle.State.RESUMED);
} androidx.lifecycle has 5 status: CREATED, RESUMED, DESTROYED, STARTED, INITIALIZED. Android Lifecycle (https://developer.android.com/guide/components/activities/activity-lifecycle) follows these steps: onCreate -> onStart -> onResume -> (App Running) -> onPause -> onStop -> onDestroy. so let's do some test: receive notification from foreground, and press home button to make app background, press notification. if the theory correct, app will shows up and logcat will print RESUMED text. but NO. CREATED was called. I mean, HOW? |
I can't figure out why. :/ anyone who're good at Android. please dig this issue more. @notifee+react-native+9.0.0.patch
public class NotifeeApiModule extends ReactContextBaseJavaModule implements PermissionListener {
private static final int NOTIFICATION_TYPE_DISPLAYED = 1;
private static final int NOTIFICATION_TYPE_TRIGGER = 2;
private static final int NOTIFICATION_TYPE_ALL = 0;
private static ReactApplicationContext context; // <- add static variable to hold ReactApplicationContext.
public NotifeeApiModule(@NonNull ReactApplicationContext reactContext) {
super(reactContext);
context = reactContext; // <- add this line
}
// make this method to get context elsewhere
public static ReactApplicationContext getContext() {
return context;
}
private static @Nullable ReactContext getReactContext() {
// ReactNativeHost reactNativeHost =
// ((ReactApplication) EventSubscriber.getContext()).getReactNativeHost();
// ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
// return reactInstanceManager.getCurrentReactContext();
return NotifeeApiModule.getContext(); // <- add this line. don't worry, ReactApplicationContext can be converted into ReactContext
}
static boolean isAppInForeground() {
return ProcessLifecycleOwner.get()
.getLifecycle()
.getCurrentState()
.isAtLeast(Lifecycle.State.CREATED); // <- change RESUME to CREATED.
} |
@helenaford can we open this again? this issue was not fixed and still persists. |
I'm on using FCM (if that matters) I can confirm only the "DELIVERED" event is detected by notifee.onBackgroundEvent (I guess they are all related) on the other hand, the "messaging().setBackgroundMessageHandler" from FCM does detect the notification in background; and the FCM messaging().onMessage also works on foreground |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
okay, I found a... very sad solution. disabling bridgeless mode definitely fixes all problems.
however this one, realllllly bad solution. after RN 0.73, bridgeless mode is enabled by default and they're ready to completely replace old one. soon or not, we cannot keep the old way. and even more worse thing. contributor of RN said "most likely deprecate and remove Headless JS in a future version". basically bridgeless mode disables any asynchronous action (aka micro task), including HEADLESS TASK. since firebase messaging also depends on this one, it... might be getting into really bad situation. |
Indeed - getting Notifee working well with new architecture (and RNFB as needed) is something I'm paying attention to |
after investigating more same issue, I can summarize this problem:
private static @Nullable ReactContext getReactContext() {
if (DefaultNewArchitectureEntryPoint.getBridgelessEnabled()) {
ReactHost reactHost =
((ReactApplication) EventSubscriber.getContext()).getReactHost();
return reactHost.getCurrentReactContext();
} else {
ReactNativeHost reactNativeHost =
((ReactApplication) EventSubscriber.getContext()).getReactNativeHost();
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
return reactInstanceManager.getCurrentReactContext();
}
}
first error is bridgeless problem. you cannot do any asynchronous js job since architecture blocks it. this cause everything wrong. - once error occurs, background event will be dead and not listening further messages. second error is AppRegistry was not treated as a Callable Module in bridgeless mode, (also pressing notification will make app status CREATED, not resumed.) so background event will eventually failed. you can find workaround here, if you're interested. |
@Eclipses-Saros your investigation and testing here, and all of the findings you posted is incredibly valuable, thank you |
I have released a fix that should be backwards compatible to older react-native versions (I tested down to 0.72) in #1127 as v9.1.2 here Please understand that there is a gigantic mix of issues above, and to my knowledge:
Now I understand that the way the package works is a little subtle and a little irritating in a few ways right now but that's not a reason to keep this issue open - we're working on addressing that elsewhere If there are further problems with getting the events to register at all please open a new issue with a reproducible example Thanks! |
Hey, thanks for building such a cool package for handling notifications in RN.
The current issue that I'm facing is that, if the app is in the foreground state and I click on the notification. The onForegroundEvent is not listening to that action, and I'm unable to receive the notification payload or any event type, rather it gives the warning that the onBackgroundEvent is not setup.
The strange thing is that, when the notification is delivered. I'm able to get a log from the onForegroundEvent, for the Event type delivered, but on clicking the notification, nothing happens.
Info
The current workaround, that I'm using is, which was suggested by @mikehardy .
Being, we store the notification in the onBackgroundEvent, in a global state management storage and then retrieve it, wherever in the app we require.
The text was updated successfully, but these errors were encountered: