expo: [expo-notifications][bare workflow] Listener registered by addNotificationResponseReceivedListener() doesn't fire when the app is closed and then opened by tapping the push notification

🐛 Bug Report

Environment

software version
expo detached to bare RN app
react-native 0.61.5
expo-notifications 0.1.1
platform OS iOS

Steps to Reproduce

According to the documentation listeners registered by addNotificationResponseReceivedListener(listener: (event: NotificationResponse) => void): void will be called whenever a user interacts with a notification (eg. taps on it). If the app was closed (not in the background) and then opened by tapping the push notification, listener registered by this method will not be called.

Expected Behavior

Callback for addNotificationResponseReceivedListener should fire on the next tick after the app starts with the notification data, in the same way as Notifications.addListener() from managed workflow works. To be able to update the UI based on the notification, or maybe navigate to a particular screen when a notification was selected.

Actual Behavior

When the user taps on push notification when the app is killed, that just launch the app and listener doesn’t trigger.

If there are any other ways to handle such a case with expo-notifications, please tell us.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 8
  • Comments: 45 (18 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same issue as @kale94 Both addNotificationReceivedListener and addNotificationResponseReceivedListener does not work in Android at all while the same code work perfectly in iOS

Tested with Android 9, Pixel 3 in Standalone build

@sjchmiela, with 0.1.6 release I’m able to receive notifications when app is in foreground in the callback in Notifications.addNotificationReceivedListener(). However, I’m still not able to receive any notifications when app is in background and opened by clicking the app icon or the notification bar.

OS: Android version 10 Device: Google Pixel 3

Any suggestions?

i maded a mistake with app.json on android

Open your app.json and add the following inside of the “expo” field:

{
  "expo": {
    ...
    "android": {
      ...
      "useNextNotificationsApi": true,
    }
  }
}

@sjchmiela addNotificationResponseReceivedListener catches push notification by pressing on which you opened the app, but only if you add listener just after the app start. When you add listener after some delay (300ms) “initial” push notification will be ignored.

So, this works

export default class App extends React.Component {
  render() {
    return null
  }

  componentDidMount() {
    addNotificationResponseReceivedListener((response) => {
      console.log('App was opened by notification: ', response.notification)
    })
  }
}

and this doesn’t

export default class App extends React.Component {
  render() {
    return null
  }

  componentDidMount() {
    setTimeout(
       () => {
          addNotificationResponseReceivedListener((response) => {
            console.log('App was opened by notification: ', response.notification)
          })
    }, 1000)
  }
}

Catching “initial” notification is needed when it has attached deep link and you need to navigate on it just after app start.

In our app we don’t attach any notification listener during the loading time (fonts, persisted state, etc.). Usually it takes 300-500ms to finish loading and then we attach notification listener. So, the bug was that this listener doesn’t catch “initial” notification. But we made a workaround and now everything works fine.

If you think that this is not a real issue feel free to close it (the main problem could be with backward compatibility with Expo.Notifications as they were handling this “delay-case”). You can also rename this issue in a more descriptive way.

P.S. Thank you for expo-notifications in general 😃

What worked for me was just writing the Notifications functions outside the main App() function.

import React from 'react';
import * as Notifications from 'expo-notifications';

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: false,
    shouldSetBadge: false,
  }),
});

Notifications.addNotificationResponseReceivedListener((response) => {
  alert('Notification message was clicked! And it worked even when the app was closed, my lucky friend!');
});

export default function App() {
   // Your code
};

It worked outside of component…

expo-notifications@0.1.3-rc.0 is now available. Note that (at least I) had to do some magic to make Xcode rebuild the Pods project — you may need to <kbd>Product</kbd> > <kbd>Clean build folder</kbd> to use the updated code (I also had to remove the app from the simulator on which I was testing the fix 🤷) (if it works for you without all this hacks, great!).

Hey, sorry for making you go through this problem, I have introduced a bug to the implementation at the last stage of developing so I didn’t notice it — I have reproduced the bug and I think I also fixed it.

Would you mind checking if applying this patch with patch-package solves the problem for you?

I will publish expo-notifications@0.1.3-rc.0 with this fix and let you know when it’s ready to be tested.