title: react-native-firebase getInitialNotification loop
body: <p>I use React Native Firebase to receive notifications in React Native.
  When message is received in background and I click on notification, getInitialNotification will be triggered. If I navigate to another screen an than back to my HomeActivity, getInitialNotification will be fired again. How can I achieve to trigger it only once?</p>
  
  <pre><code>import React, { Component } from 'react';
  import { StyleSheet, Platform, View, Text, YellowBox, Button } from 'react-native';
  import StyleIndex from './css/StyleIndex';
  
  import firebase from 'react-native-firebase';
  import type { Notification, NotificationOpen } from 'react-native-firebase';
  
  
  export default class HomeActivity extends Component {
    state = { currentUser: null }
  
  componentDidMount() {
      const { currentUser } = firebase.auth()
      this.setState({ currentUser })
  
      var collectionReference = firebase.firestore().collection('users');
      var query = collectionReference.where("phone", "==", currentUser.phoneNumber);
      // console.log(firebase.messaging().getToken());
      firebase.messaging().hasPermission()
      .then(enabled =&gt; {
        if (enabled) {
          firebase.messaging().getToken().then(token =&gt; {
            query.get().then(function(querySnapshot) {
              if (querySnapshot.size == 1) {
                firebase.firestore().collection('users').doc(querySnapshot.docs[0].ref.id).update({
                  fcmtoken: token,
                })
              } else {console.log('no or more than one documents found')}
            });
          })
          // user has permissions
        } else {
          firebase.messaging().requestPermission()
            .then(() =&gt; {
              alert("User Now Has Permission");
              console.log('now has permission');
  
            })
            .catch(error =&gt; {
              alert("Error", error)
              // User has rejected permissions  
            });
        }
      });
      this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(fcmToken =&gt; {
        query.get().then(function(querySnapshot) {
          if (querySnapshot.size == 1) {
            firebase.firestore().collection('users').doc(querySnapshot.docs[0].ref.id).update({
              fcmtoken: fcmToken,
  
  
            })
            .catch(error =&gt; {
              alert("Error", error)
              // User has rejected permissions  
            });
        }
      });
      this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(fcmToken =&gt; {
        query.get().then(function(querySnapshot) {
          if (querySnapshot.size == 1) {
            firebase.firestore().collection('users').doc(querySnapshot.docs[0].ref.id).update({
              fcmtoken: fcmToken,
            })
          } else {console.log('no or more than one documents found')}
        });
    });
  
   // DEFAULT CHANNEL
   const channel = new firebase.notifications.Android.Channel('default', 'Default Channel', firebase.notifications.Android.Importance.Max);
   channel.setDescription('My default channel');
   // Create the channel
   firebase.notifications().android.createChannel(channel);
  
   // data-only messages from FCM
   // https://rnfirebase.io/docs/v4.2.x/messaging/receiving-messages
   this.messageListener = firebase.messaging().onMessage((message) =&gt; {
     // Process your message as required
     console.log('onMessage', message);
   });
  
   // LOCAL NOTIFICATION: FOREGROUND
   this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) =&gt; {
     // Process your notification as required
     // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
     console.log('onNotificationDisplayed', notification);
   });
  
   this.notificationListener = firebase.notifications().onNotification((notification: Notification) =&gt; {
     // Process your notification as required
     console.log('onNotification', notification);
     notification.android.setChannelId('default');
     notification.android.setAutoCancel(true);
     firebase.notifications().displayNotification(notification);
   });
  
  
   // APP FOREGROUND / BACKGROUND
   this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) =&gt; {
     // Get the action triggered by the notification being opened
     const action = notificationOpen.action;
     console.log('onNotificationOpened - action', action);
     // Get information about the notification that was opened
     const notification: Notification = notificationOpen.notification;
     notification.android.setChannelId('default');
  
    //  firebase.notifications().displayNotification(notification);
     console.log('onNotificationOpened - notification', notification);
  
   firebase.notifications().displayNotification(notification)
   });
  
       // APP CLOSED
       firebase.notifications().getInitialNotification()
       .then((notificationOpen: NotificationOpen) =&gt; {
         if (notificationOpen) {
           // App was opened by a notification
           // Get the action triggered by the notification being opened
           const action = notificationOpen.action;
           console.log('getInitialNotification - action', action);
           // Get information about the notification that was opened
           const notification: Notification = notificationOpen.notification;
          //  notification.android.setChannelId('default');
           notification.android.setAutoCancel(true);        
           console.log('getInitialNotification - notification', notification);
           // Display the notification
          //  firebase.notifications().displayNotification(notification);
          alert('getInitialNotification');
           firebase.notifications().removeAllDeliveredNotifications();
           firebase.notifications().cancelAllNotifications();
         }
       });
  
    }
    componentWillUnmount() {
      this.onTokenRefreshListener();
      this.notificationDisplayedListener();
      this.notificationListener();
      this.notificationOpenedListener();
  }
  </code></pre>