Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

onNotification does not fire when tapping notification and app is in background with activity "closed" #1341

Closed
ws333 opened this issue Mar 12, 2020 · 8 comments

Comments

@ws333
Copy link

ws333 commented Mar 12, 2020

I only use local notifications and everything works fine (i.e. the onNotification callback fire when tapping a notification) except for when tapping a notification and the app is in the background after using the back button to "close" the app. It works fine when the app is dead, when the app is in the foreground or in the background while an activity is "open".

This is the log from Android Studio after tapping the back button to "close" the app and then the last line that probably hints to the solution appear when tapping the notification:

Update: The following appear in the log also when AppState change to 'active', i.e. when tapping the launcher icon etc.

W/libEGL: EGLNativeWindowType 0x7a4db0c010 disconnect failed
W/System: A resource failed to call response.body().close(). 
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@c41a823

Relevant info from package.json:

    "react": "16.9.0",
    "react-native": "0.61.5",
    "react-native-push-notification": "^3.1.9"

I´ve tried the following suggestions from other posts here without resolving the issue:
Adding android:launchMode="singleTask" to <activity android:name=".MainActivity" ... in AndroidManifest.xml as suggested in #1272
Moving PushNotification.configure outside of React components #652

I'm currently researching this issue by debugging with Android Studio and learning what I need to fix it, but are quite new to Android development and java so any feedback on the possible cause of this is highly appreciated.

Update: When testing a release build the onNotification callback did not fire even on app load. That is why I went for a different workaround described in a post below. Thought it is a special case workaround that's probaly not for most use cases.

@ferprez
Copy link

ferprez commented Mar 16, 2020

Same here. Did you find any workaround to this?

@ws333
Copy link
Author

ws333 commented Mar 17, 2020

@ferprez, I did find a workaround that seems to fix the issue for me both on android and iOS. I don't use the onNotification callback at all anymore, but store the information I need with AsyncStorage and retrieve it on app load and on AppState change. It will display the stored data even when the notification is rejected and AppState change to 'active' later though, so not a workaround for most use cases, but for my current app that is what I want, so didn't dive into a possible fix for that. When testing a release build the onNotification callback did not fire even on app load, so I began to research other options.

Here is my current solution:

  • Just before/after PushNotification.localNotificationSchedule() use @react-native-community/async-storage to store the notification data needed for retrieval later, like a text and the notification time.

  • Create a function e.g. displayStoredNotification() that displays the notification when the stored time is reached and then removes the stored notification.

  • Execute displayStoredNotification() in AppState change handler when state is 'active' as this will trigger on both app load and app transitioning from the background.

I think I've described the basic setup, though I also use contexts/redux and have to make sure that the redux store is initialized from stored data before I execute displayStoredNotification() so there is a bit more logic involved.

I also found that I need to add android:launchMode="singleInstance" after <application android:name=".MainApplication" in AndroidManifest.xml else I would often get multiple instances of React when switching between foreground/background and this would create multiple AppState change listeners and strange navigation behaviour.

@cx5168
Copy link

cx5168 commented Apr 21, 2020

"react": "16.9.0",
"react-native": "0.61.4",
"react-native-push-notification": "^3.1.9"
"@react-native-community/push-notification-ios": "1.1.1"

onNotification is not work.

@Dallas62
Copy link
Collaborator

Hi,
Can you try the version on master, this might be a bug with the default channel not defined

@ws333
Copy link
Author

ws333 commented Apr 29, 2020

@Dallas62 If you meant to try master as a fix for the original notification issue I can do that in a few days and give you a feedback.

@Dallas62
Copy link
Collaborator

A released as been made 3.3.0.

@ws333
Copy link
Author

ws333 commented Apr 30, 2020

Hi, I created a new React Native CLI project with RNPN v3.3.0 and set up AndroidManifest.xml to support localNotificationSchedule() as specified in the installation guide

Then I get this error screen:
Screenshot_20200430-132630_push_notif_testing

@Dallas62
Copy link
Collaborator

Dallas62 commented Apr 30, 2020 via email

@Dallas62 Dallas62 closed this as completed May 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants