React native - How to navigate on Push notification click?
Solution 1:
This is what you can do. Firstly, you can initialize and export navigationRef from the file in which you've NavigationContainer.
// App.js
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { navigationRef } from './RootNavigation';
export const navigationRef = React.createRef();
export function navigate(name, params) {
navigationRef.current?.navigate(name, params);
}
export default function App() {
return (
<NavigationContainer ref={navigationRef}>{/* ... */}</NavigationContainer>
);
}
then you can import it in which you've PushNotification.configure()
// notification-service.js
import PushNotification from 'react-native-push-notification';
import { navigationRef } from './App';
/* Your other code */
PushNotification.configure({
onNotifications: (notification) => {
// Now you'll have access to the navigation object's function here...
}
})
You can get more clarity from the docs here: navigating-without-navigation-prop