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
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:
But here is the catch, notification data send to firebase needs to look like this:
You have to use only
data
part of notification, when you usetitle
orbody
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 removedtitle
andbody
keys from my notification and kept them underdata
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
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 eventNote: 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:
@Codelica @appsgenie I tried multiple ways as suggested but nothing is working with android real device. Please share a working code if any workaround.