4

I have a problem regarding the following code sample:

Notifications.setNotificationHandler({//makes sure notification is displayed even when app is open, nothing else
    handleNotification: async (notification) => {
        //const value = await AsyncStorage.getItem('presetlanguage');
        //console.log("ASYNC STORAGE LANGUAGE FROM OUTSIDEEEE: ", value)
        //if(notification.request.content.body == "You received a new letter from a PigeonBuddy!"){
        //    console.log("hat geklappt")
        //}
        return{
            shouldShowAlert: true
        };
    }
});

const MainScreen = props => {
    const dispatch = useDispatch();
    var chosenLanguage = useSelector(state => state.myLanguage.myLanguage); ...........

The setNotificationHandler is responsible for handling incoming notifications and therefore I want to filter my incoming notifications. For example, depending on which screen I am on, I want to either display the notification or not display it. The problem however is, I have neither access to my navigation state nor to my redux states because this handling of the notifications happens outside the default screen main function which covers all variables and which also uses props through navigations. It is forbidden to call redux hooks there and also I have no access to my navigation state because I have no access to my props variable which I get through navigation.

How can I display my notifications then depending on which screen I am on? How are companies like Facebook doing it? If you're on a chat screen you don't get notifications but if you are outside a notification "New message received from ..." is displayed.

Brijesh Kalkani
  • 789
  • 10
  • 27

1 Answers1

2

You can export a navigation ref

export const navRef = React.createRef();

const App = () => {
  return (
    <NavigationContainer ref={navRef}>
      ...
    </NavigationContainer>
  );
};

and use it to get the screen you are in, inside the setNotificationHandler

const currentRouteName = navRef.current.getCurrentRoute().name;

can use similar code. this code is for react-navigation v6

Edited

for React-navigation v4 create a reactRef that keeps track of current screen utilising the onNavigationStateChange.

export const currentScreenRef = React.createRef('Splash');

function getActiveRouteName(navigationState) {
  if (!navigationState) {
    return null;
  }
  const route = navigationState.routes[navigationState.index];
  if (route.routes) {
    return getActiveRouteName(route);
  }
  return route.routeName;
}

const App = () => {
  return (
    <NavigationContainer 
      onNavigationStateChange={(prevState, currentState) => {
        const currentRouteName = getActiveRouteName(currentState);
        currentScreenRef.current = currentRouteName;
      }}
    >
      ...
    </NavigationContainer>
  );
};

so will be able to call it as

const currentRouteName = currentScreenRef.current;

NB: not tested as do not have any projects running V4. If not working or needs edit, add a comment

Muhammed Yasir MT
  • 1,894
  • 1
  • 6
  • 7
  • Thanks for the answer sir, I will try to implemement this as you said as soon as possible and if everything works I will let you know here! – Kubaghetto the fresh Testobun Sep 02 '21 at 17:24
  • This solution only works for react navigation 6, the problem is I am using react navigation 4. Im currently struggling to implement this solution. – Kubaguette The Testobun Sep 06 '21 at 20:39
  • 1
    I will try to update the solution to include v4 today – Muhammed Yasir MT Sep 07 '21 at 00:58
  • Placed all the 3 functions in the same file where I have the navRef, exported getCurrentRouteName() and tried to call it outside the file through importing it but it returns undefined, but I came up with an other solution. Will edit your post a little bit. Added now a solution which uses a Listener from navigation v4, seems to work for me! – Kubaguette The Testobun Sep 07 '21 at 10:57
  • 1
    Got the edit and i thought, it would be easier to keep track of screen in one place and came up with the solution added in the edited section – Muhammed Yasir MT Sep 07 '21 at 11:38
  • Got it champ, no problem! I did not even know that you need to approve the edit, probably because you are the bounty owner. – Kubaguette The Testobun Sep 09 '21 at 10:49