Open leonardo409188 opened 2 years ago
@leonardo409188 just working on it. Have you found solution? I ve read in other issue that need to use react-native-notifications to handle such cases.
@leonardo409188 can't you use https://reactnative.dev/docs/appstate to check if the app is in foreground on Android and if so - do not navigate? Or it always results in 'active'? Did you manage to find a way to differentiate between the states?
@hypnocill
I'm currently using appstate to manage this, but it behaves a bit strangely:
When I get a notification in the background and I click on it to open the App, when opening the App the appstate changes to 'active' and always does the 'active' action (in my case I created a custom component).
so I had to do a little hack:
Before changing the appState I put a setTimout to give time to do the background action before going to 'active'
AppState.addEventListener('change', nextAppState => { if (appState.current.match(/inactive|background/) && nextAppState === 'active') { setTimeout(() => { appState.current = nextAppState; }, 1500); return; } appState.current = nextAppState });
So in handleNavigation I use like this:
if (appState === 'active') { showNotificationComponent(); // show custom component } else { handleNavigateNotification(); // navigate to a screen }
@leonardo409188 Thanks a lot for answering. It seems there's no other way to properly detect which state did the notification came from. One thing I'm wondering is if node_modules/react-native-pusher-push-notifications/android/src/main/java/com/b8ne/RNPusherPushNotifications/NotificationsMessagingService.java on line 57 corresponds to this case (notification opened from background) and if we can pass an additional property in the notificationEvent payload which we can check in js.
If I find enough time to try this out, I'll let you know here
@leonardo409188 I managed to make a differentiation between notification coming in foreground and others. In node_modules/react-native-pusher-push-notifications/android/src/main/java/com/b8ne/RNPusherPushNotifications/PusherWrapper.java before line 88, add: map.putString("appState", "active");
This is adding the appstate flag with 'active' value for visible activity and can be used in the JS listener -> if notification?.appState !== 'active' -> navigate
I think I'll use npm patch-package to patch it locally for now
Still not sure why opening notification from killed state doesn't trigger the JS event listener despite the native event being fired
If Pusher-JS not mandatory for you, please take a look to the lib below. It has some nice methods to detect some cases. `// Optional: Flow type import type { Notification, NotificationOpen } from 'react-native-firebase';
async componentDidMount() { const notificationOpen: NotificationOpen = await firebase.notifications().getInitialNotification(); if (notificationOpen) { // App was opened by a notification // Get the action triggered by the notification being opened const action = notificationOpen.action; // Get information about the notification that was opened const notification: Notification = notificationOpen.notification; } }`
Is there any way I can trigger the handleNotifications function only when I open the notification by clicking the push?
I need to navigate to another screen only when the person clicks on the push, but currently if the app is open, it also navigates. it only happens on Android since for iOS the notification content returns the appState