0

I am trying to redirect to another screen when a notification is received in App.js. I am using useNavigation() but it is not working.

Is there a way to redirect to another screen or use the notifications in another way?

export default function App() {
  function UserScreen() {  
    const navigation = useRef(useNavigation());  
    navigation.navigate('Chat');
  }


  useEffect(() => {
    const backgroundSubscription = Notifications.addNotificationResponseReceivedListener(
      (response) => {
        switch (response.request?.content.data.type) {
          case 'chat':
            return UserScreen()
          default:
            return console.log('Entro al default')
        }
      }
    );

    const foregroundSubscription = Notifications.addNotificationReceivedListener(
      (notification) => {
        switch (notification.request?.content.data.type) {
          case 'chat':
            return UserScreen()
          default:
            return console.log('Entro al default')
        }
      }
    );

    return () => {
      backgroundSubscription.remove();
      foregroundSubscription.remove();
    };
  }, []);

  return (
    <Provider store={store}>
      <SparkitNavigator />
    </Provider>

  );
}

1 Answers1

0

How about:

export default function App() {
  const navigation = useNavigation(); 
  
  function UserScreen() {  
   navigation.navigate('Chat');
  }
  // rest code
}

Make sure App and Chat is registered in the Navigator Stack

Ngo Van
  • 857
  • 1
  • 21
  • 37