NotificationResponseReceivedListener not called when app is killed
See original GitHub issueSummary
I ran into problems while developing an app that has to respond to push notifications. I based this app on the expo notification snack that is found in the documentation.
When trying out my app, the expo notification service works fine when an app is in the foreground or background, but does not trigger when the app is killed. I use the addnotificationresponsereceivedlistenerlistener method to handle notifications when the app is killed.
I found a lot of issues about this problem that have already been closed. Unfortunately, the solutions/pull request in these issues don’t solve the problem on my system. I therefore think this might be a bug that was introduced in version 42 of the expo SDK.
Related issues
- https://forums.expo.dev/t/push-notification-doesnt-open-specific-screen-when-app-is-killed-works-only-in-foreground-and-background/53890/5
- https://github.com/expo/expo/issues/13333
- https://github.com/expo/expo/issues/12954
- https://github.com/expo/expo/issues/12228
- https://github.com/expo/expo/issues/11933
- https://github.com/expo/expo/issues/11470
- https://github.com/expo/expo/issues/11340
- https://github.com/expo/expo/issues/10621
- https://github.com/expo/expo/issues/9968
- https://github.com/expo/expo/issues/6943
Managed or bare workflow? If you have ios/
or android/
directories in your project, the answer is bare!
managed
What platform(s) does this occur on?
Android
SDK Version (managed workflow only)
42.0.1
Environment
Expo CLI 4.10.0 environment info:
System:
OS: Linux 5.11 Ubuntu 20.04.2 LTS (Focal Fossa)
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 14.17.1 - ~/.nvm/versions/node/v14.17.1/bin/node
npm: 6.14.13 - ~/.nvm/versions/node/v14.17.1/bin/npm
IDEs:
Android Studio: AI-203.7717.56.2031.7583922
npmPackages:
expo: ~42.0.1 => 42.0.3
react: 16.13.1 => 16.13.1
react-dom: 16.13.1 => 16.13.1
react-native: https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz => 0.63.2
react-native-web: ~0.13.12 => 0.13.18
npmGlobalPackages:
expo-cli: 4.10.0
Expo Workflow: managed
Reproducible demo or steps to reproduce from a blank project
Development mode
- Clone the example project or create a new expo project using the push notification snack in the expo-notifications documentation.
- Install the NPM dependencies.
- Run the app on an android emulator or a real android device.
- Kill the app while keeping the expo go app open and send a push notification using the push notification tool.
- Click the notification.
- See that the alert is not triggered.
Production mode
- Clone the example project or create a new expo project using the push notification snack in the expo-notifications documentation.
- Setup your google FCM credentials (see the expo documentation).
- Build the project
.apk
using theeas build -p android --profile preview
command. - Install the app on your device using the
adb
tool. - Open the app.
- Close the app.
- Send a notification using the push notification tool.
- Click the notification.
- See that the alert is not triggered.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:93 (29 by maintainers)
There seems to a be a lot of confusion in this thread, understandably.
@Codelica To clarify things for people reading this (as of Expo SDK 46):
Notifications.addNotificationResponseReceivedListener()
for tapped notifications when the app is killed will be null. This is a known limitation of using Expo Go.Notifications.addNotificationResponseReceivedListener()
when a user taps on a notification is always valid.Notifications.addNotificationResponseReceivedListener()
will be null if the user taps a notification when the app is killed.As many have stated in this thread, for getting Android notifications working when the app is killed, you need to move
Notifications.addNotificationResponseReceivedListener()
to the top-level namespace out of the component tree. For most people, this meansApp.js
.The example that @jackkinsella shared works fine: https://github.com/expo/expo/issues/14078#issuecomment-1041294084 . Just adapt it as necessary to work with your app’s design.
We have tested this on iOS/Android SDK 46 using EAS builds for our production apps and it works fine. The docs could use with being updated to acknowledge this issue, since its been around for ages, and obviously still confuses the shit out of everyone 🙃
So the reason behind this is that the native event is being sent before the actual listener is available. This is why the
useLastNotificationResponse
hook was created, but I suppose it would be easier to have the listener be patched without the need for more user code.One potential fix that I’ve tested is to manually trigger the emitter after adding it with the result of
getLastNotificationResponseAsync
if it’s not null. To try out this fix, I’ve created a gist containing the diff you should apply toexpo-notifications
innode_modules
(if you’ve usedpatch-package
before, you can just download this file directly and place it in yourpatches
directory). I haven’t tested this extremely thoroughly yet, so I would use caution before pushing this out to production code, but if you can try it and let me know how it works for you, that would be helpful!