question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NotificationResponseReceivedListener not called when app is killed

See original GitHub issue

Summary

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

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

  1. Clone the example project or create a new expo project using the push notification snack in the expo-notifications documentation.
  2. Install the NPM dependencies.
  3. Run the app on an android emulator or a real android device.
  4. Kill the app while keeping the expo go app open and send a push notification using the push notification tool.
  5. Click the notification.
  6. See that the alert is not triggered.

Production mode

  1. Clone the example project or create a new expo project using the push notification snack in the expo-notifications documentation.
  2. Setup your google FCM credentials (see the expo documentation).
  3. Build the project .apk using the eas build -p android --profile preview command.
  4. Install the app on your device using the adb tool.
  5. Open the app.
  6. Close the app.
  7. Send a notification using the push notification tool.
  8. Click the notification.
  9. See that the alert is not triggered.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:93 (29 by maintainers)

github_iconTop GitHub Comments

5reactions
nakedguncommented, Sep 13, 2022

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):

  • Expo Go - notifications when the app is backgrounded, foregrounded and killed are all received, but the response forNotifications.addNotificationResponseReceivedListener() for tapped notifications when the app is killed will be null. This is a known limitation of using Expo Go.
  • iOS - for EAS builds: notifications when the app is backgrounded, foregrounded or killed are all received and the response from Notifications.addNotificationResponseReceivedListener() when a user taps on a notification is always valid.
  • Android - for EAS builds: notifications when the app is backgrounded, foregrounded or killed are all received but the response from 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 means App.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 🙃

5reactions
cruzachcommented, Nov 2, 2021

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 to expo-notifications in node_modules (if you’ve used patch-package before, you can just download this file directly and place it in your patches 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expo notification doesnt fire method when app is killed/ ...
The problem is that the useEffect() get called too late after the app has finished initializing. Therefore the listener is not added before ......
Read more >
[expo-notifications] No notification data if app is killed or ...
Hi, we have sending/receiving the notifications working in our standalone builds. However, we're trying to detect when our app has been ...
Read more >
Adding notifications to your expo app
This listener is fired whenever a user taps on or interacts with a notification // (works when app is foregrounded, backgrounded, or killed) ......
Read more >
[Expo] Android notifications are not received in-app when it's ...
First of all: the authors changed the notification payload. In the new version, there are two listeners for receiving notification and receiving ...
Read more >
Smack 4.4.4: pingFailed not being called
It looks like the ping failure event listener does not fire, if between the time that the ... If in your setup, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found