Zaid Qureshi
Zaid Qureshi

Reputation: 175

how to navigate to specific screen when press the notification in react native

I'm using react native push notification with firebase. I'm getting notifications and everything is good, but the issue is OnOpenNotfication function is firing up two times.It's showing {} the first time and its calling itself automatically. When I press on the notfication then it also fires and showing nothing I mean in notify there is nothing. But I want to call onOpenNotification only when I click on the notification.And I also want to navigate the user from onOpenNotfication to specific screen but it fires up automatically so that's why I can't Here is the code.

 componentDidMount = async () => {
   
    fcmService.registerAppWithFCM();
    fcmService.register(onRegister, onNotification, onOpenNotification);
    localNotificationService.configure(onOpenNotification);

    function onRegister(token) {
      console.log('[App] onRegister: ', token);
    }

    function onNotification(notify) {
      console.log('[App] onNotification: ', notify);
      const options = {
        soundName: 'default',
        playSound: true, //,
        // largeIcon: 'ic_launcher', // add icon large for Android (Link: app/src/main/mipmap)
        // smallIcon: 'ic_launcher' // add icon small for Android (Link: app/src/main/mipmap)
      };
      localNotificationService.showNotification(
        0,
        notify.title,
        notify.body,
        notify,
        options,
      );
    }

    function onOpenNotification(notify) {
      console.log('[App] onOpenNotification: ', notify);
      alert('Open Notification: ' + notify.body);
    }
  };

Upvotes: 0

Views: 753

Answers (1)

Rehan Yahya
Rehan Yahya

Reputation: 34

I hope it will help you. You can try using this logic.

 import firebase from 'react-native-firebase';
    
    componentDidMount(){
        firebase
            .notifications()
            .onNotificationOpened((notificationOpen: NotificationOpen) => {
              console.log('onNotificationOpened', notificationOpen);
              this.handleNotification(notificationOpen.notification, false);
            });
    }
    
    handleNotification(notification: Notification, appClosed){
       //notification will have your custom data and here you can apply your logics
       //you can use navigation here according to your logic.
       navigatorRef.navigate("routeName");
    }

Upvotes: 1

Related Questions