expo: SDK 45.0.8 uselastnotificationresponse is not working when app killed/closed

Summary

On real android device, tap to notification is not navigating to specific screen. Did some debugging and found uselastnotificationresponse is not called when app is in close/ killed.

Note: It is working when app in foreground.

“expo-notifications”: “~0.15.4”

What platform(s) does this occur on?

Android

Environment

expo-env-info 1.0.5 environment info: System: OS: Windows 10 10.0.19044 Binaries: Node: 14.17.5 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.11 - ~\AppData\Roaming\npm\yarn.CMD npm: 6.14.14 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: Version 2020.3.0.0 AI-203.7717.56.2031.7583922 npmPackages: expo: ^45.0.8 => 45.0.8 react: 17.0.2 => 17.0.2 react-dom: 17.0.2 => 17.0.2 react-native: 0.68.2 => 0.68.2 react-native-web: 0.17.7 => 0.17.7 Expo Workflow: bare

Minimal reproducible example

Use Firebase Cloud Messaging(FCM) notification to send notification Click on notification when app is closed Use this code to handle click:

 React.useEffect(() => {
    if (
      lastNotificationResponse &&
      lastNotificationResponse.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER
    ) {
      try {

        const req = lastNotificationResponse.notification.request;

        let params = {};
        var content;
        if (req.trigger.remoteMessage) {
          content = req.trigger.remoteMessage;
        } else {
          content = req.content
        }

        if (content.data.navigation_screen) {
          params[content.data.navigation_param] = content.data.navigation_param_value;
          props.props.navigation.navigate(content.data.navigation_screen
            , params);
        } else {

          props.props.navigation.navigate("Notifications");
        }

      
      } catch (e) {

        Logger.Error(e, "TapHandler");
      }
    }
  }, [lastNotificationResponse]);

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 30

Most upvoted comments

uselastnotificationresponse is not working when app is killed/closed with android real device(sdk46). Please fix it asap. @expo-community

I was able to get this working and read data from the notification on both Android and iOS devices. In my Android application I have this piece of code:

// App.js
import * as Notifications from 'expo-notifications';
import { useEffect } from 'react';

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

export default function App() {
  const lastNotificationResponse = Notifications.useLastNotificationResponse();

  useEffect(() => {
    if (!lastNotificationResponse) return;

    let data = null;

    if (Platform.OS === 'android') {
      data = lastNotificationResponse?.notification?.request?.trigger?.remoteMessage?.data;
    } else {
      data = lastNotificationResponse?.notification?.request?.trigger?.payload?.data;
    }

    console.log(data);
  }, [lastNotificationResponse]);


  return <></>;
}


But here is the catch, notification data send to firebase needs to look like this:

def firebase_notification
  {
    data: {
      title: title,
      message: message,
      badge: user.notifications.unread.count,
      link: url,
      data: {
        link: url
      }
    },
    android: {
      priority: 'normal'
    }
  }
end

You have to use only data part of notification, when you use title or body in your notification, the data read in JS hook will be undefined. I don’t know why it’s behaving this way, but after I’ve removed title and body keys from my notification and kept them under data the notification hook started working and returned correct notification data.

Please note that you can’t test this from Firebase Cloud Console, because google requires you to fill title of notification, but when you fill the title, payload will have title key and ExpoNotification will fail to read the data.

Most important part was to send notification in correct format. However if you don’t have control over sended data, I think there’s currently no way to fix this.

uselastnotificationresponse is not working when app is killed/closed with android. Neighter is addNotificationResponseReceivedListener in Android.

@Codelica using expo 46, sharing raw code

let androidOffNotificationListener;
if (Platform.OS !== "android") {
  androidOffNotificationListener = Notifications.addNotificationResponseReceivedListener(
    ({ notification }) => {
       alert('We received a touched notification event in Android');
    }
  );
}

class App extends React.PureComponent {
async componentDidMount() {}
 render() {return (<MyComponent/>}}

}
export default App;

App is getting open when tap the notification but notification event is not triggered. Also, I have tried Notifications.useLastNotificationResponse()
This is returning null when app is killed/close However, It is working fine with ios and expo go …getting issue android real device.

@Codelica Sorry !! last time, I posted wrong code however i was using condition Platform.OS == "android" Now, To be double sure i tested again but still not receiving the event

let androidOffNotificationListener;
if (Platform.OS == "android") {

  androidOffNotificationListener=Notifications.addNotificationResponseReceivedListener(
    ({ notification }) => {
     alert('We received a touched notification event in Android');
    }
  );
}

Note: I am FCM to send notification. This is critical issue for my app. Could you please help me to get it fixed.? If you can create a sample app repo using working code to receive notifications and when touch the notification then navigate to specific screen. It would be really helpful. Thanks in advance. My Env info:

 npmPackages:
      expo: ^46.0.15 => 46.0.15
      react: 18.0.0 => 18.0.0
      react-dom: 18.0.0 => 18.0.0
      react-native: 0.69.6 => 0.69.6
      react-native-web: ~0.18.7 => 0.18.9

    Expo Workflow: bare

"expo-notifications": "~0.16.1"

@Codelica @appsgenie I tried multiple ways as suggested but nothing is working with android real device. Please share a working code if any workaround.