react-native-fcm: Remote notification is empty when I call FCM.getInitialNotification

I am using react-native-fcm ^6.0.2, react native 0.41.2 on an android device.

This is the scenario:

In my app I have a class named LoadingScreen. This class is just waiting for a push notification to change to the next scene “FinalScreen”. When the app is visible (foreground), if I send a push using the fcm console, the app receive that push and everything works perfect. The problem is: if the app goes to sleep or goes to second plane, when I send the push, the device show me the push in the notifications area, but when I clicked in it, the app shows me the LoadingScreen and nothing happens. The “notif” object is almost empty. If I do something like notif.fcm(in the js console), it only contains the action key.

This is the code of the LoadingScreen.

componentDidMount() {
    _this     = this;
    prevState = this.props.prevState;
    
    //Set some stuff
    this.setState(this.state);

    this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
      
      if(notif.fcm.body !== undefined){
        Vibration.vibrate();
        Actions.chargeFinalStep({prevState: _this.state});  
      }    
    });

    FCM.getInitialNotification().then((notif)=>{
      console.log("FCM.getInitialNotification");
      console.log(notif)
    });

    this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => {
        console.log(token)
        // fcm token may not be available on first load, catch it here
    });
    }

Any help would be appreciated 😃

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 21 (8 by maintainers)

Most upvoted comments

  1. When app is in background, the notification goes in FCM.on callback. getInitialNotification won’t have the value. To be clear Initial means launching the app, not opening the app. So I think the notification should trigger on callback. Check if it is the case.
  2. App stops rendering couple seconds after going to background, so your code to change screen may be ignored. (however I think javascript itself is still live) Try to persist the state and change screen after AppState change.