0
  • How to make navigation to certain View/Screen in Mobile App, when notification received when mobile application is closed ?
  • Platform react native v0.61.5 notification package =>

react-native-firebase v6.2.0 react-native-push-notification v3.1.9

react-navigation v4.0.10

2 Answers2

0

You can use NavigationActions from react-navigation.

App.js

<AppContainer
  ref={navigatorRef => {
  NavigationService.setTopLevelNavigator(navigatorRef);}}/>

NavigationService.js

import { NavigationActions, StackActions } from 'react-navigation';

let _navigator;

function setTopLevelNavigator(navigatorRef) {
  _navigator = navigatorRef;
}

function navigate(routeName, params) {
 _navigator.dispatch(
    NavigationActions.navigate({
    routeName,
    params,
    })
  );
 }

export default {
  navigate,
  setTopLevelNavigator
 }

Now in onNotificationOpened function from firebase you have to write one line.

NavigationService.navigate('your screen', { param:paramValue })
0

//You can send data with the notification in this format

{ "to" : "FCM_token", "collapse_key" : "type_a", "notification" : { "body" : "Testing", "title": "Test Title"

}, "data" : { "name" : "xyz" } }

//Now you can get data in listener

const notificationOpen = await firebase.notifications().getInitialNotification();

if (notificationOpen) {

    let {name} = notificationOpen.notification._data;
    
    console.log("Notification data - ", name);
    setInitialRoute(name);

}

//Now to declare state variable and change the initialRoute of the navigator....

const [initialRoute, setInitialRoute] = useState('ABC');

setInitialRoute(name); // in the notification listener

//Now use in Navigation container

  <Stack.Navigator initialRouteName={initialRoute}>

  </Stack.Navigator>