I have an Expo/React Native app that receives Firebase Messaging notifications. Two handlers for the Firebase messages use expo-notifications to present a local notification with data from the remote push notification. This part works as expected.
If a user clicks on certain notifications, they need to be re-routed to specific screens. To do this, I have tried using Notifications.getLastNotificationResponseAsync, Notifications.useLastNotificationResponse, and Notifications.addNotificationResponseReceivedListener. Notifications.useLastNotificationResponse works somewhat; only on Android when the app is in the foreground, and the app crashes the first time a notification is pressed. It has to be pressed a second time, then the app opens and re-routes. Notifications.useLastNotificationResponse is not working on iOS, even in the foreground, and the other two did not work at all for either OS. I need this to work on both platforms and in all app states (killed, foreground, and background) without crashing.
Here is a simplified version of the current code I have for this:
const lastNotification = Notifications.useLastNotificationResponse();
useEffect(() => {
if (
lastNotification &&
lastNotification.notification &&
lastNotification.actionIdentifier ===
ExpoNotifications.DEFAULT_ACTION_IDENTIFIER
) {
const notification = lastNotification.notification;
console.log("Notification response");
const screen = notification.request.content.data.screen;
navigation.navigate(screen);
}
}, [lastNotification]);