0

I am currently using FCM React Native Firebase, where I want to move the screen or take action when I open the fcm notification, like the image below :

rea

zidniryi
  • 1,212
  • 3
  • 15
  • 36

1 Answers1

0

react-native-fcm is now deprecated and no longer maintained. It is better to move to react-native-firebase.

Below shows example for react-native-firebase.

First you need to create a notification listener in your application. Mostly it is better to add it on top level root component.

You can passed relevant data on you notification payload data object. https://firebase.google.com/docs/cloud-messaging/concept-options

import firebase from 'react-native-firebase';

componentDidMount() {
      this.createNotificationListeners();
}

componentWillUnmount() {
    // Remove all the notification related listeners on unmounting the main dashboard
    if (this.notificationOpenedListener) {
      this.notificationOpenedListener();
    }
}

  /**
   * Contains all the listeners related to the Firebase notification services.
   */
  async createNotificationListeners() {
    const handleNotification = notificationOpen => {
      // Do what ever do you want, based on your notification payload
    };

    /*
     * If app is in background, listen for when a notification is clicked / tapped / opened as follows:
     * */
    try {
      this.notificationOpenedListener = firebase
        .notifications()
        .onNotificationOpened(notificationOpen => {
          console.log(
            'FirebaseDataReceiver remote notification clicked from background :',
            notificationOpen,
          );
          handleNotification(notificationOpen);
        });
    } catch (e) {
      console.log(
        'Error while clicking the remote notification from background :',
        e,
      );
    }

    /*
     * If app is closed, check if it was opened by a notification being clicked / tapped / opened as follows:
     * */
    try {
      const notificationOpen = await firebase
        .notifications()
        .getInitialNotification();
      if (notificationOpen) {
        console.log(
          'FirebaseDataReceiver remote notification clicked app start up :',
          notificationOpen,
        );
        handleNotification(notificationOpen);
      }
    } catch (e) {
      console.log(
        'Error while clicking the app was initiated by a remote notification :',
        e,
      );
    }
  }
EL173
  • 797
  • 9
  • 22