react-native-fcm icon indicating copy to clipboard operation
react-native-fcm copied to clipboard

Remote notification is empty when I call FCM.getInitialNotification

Open daniel00k opened this issue 9 years ago • 21 comments

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 :)

daniel00k avatar Mar 04 '17 17:03 daniel00k

+1

ddobby94 avatar Mar 08 '17 22:03 ddobby94

  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.

evollu avatar Apr 11 '17 20:04 evollu

I am getting undefined notification. Using FCM.getInitialNotification().then((notification) => {}). Do somebody knows why?

react-native-fcm 7.0.0 RN 0.44.0 React 16.0.0-alpha.6 iOS 10.2

cmedinasoriano1 avatar Jun 06 '17 16:06 cmedinasoriano1

@evollu If the app was opened from app icon and there was no new notification sent. what FCM.getInitialNotification() should return? Because now it always returns empty notification with opened_from_tray: 1

veedeo avatar Jun 11 '17 14:06 veedeo

Depends on OS. It should return undefined or empty object.

evollu avatar Jun 11 '17 15:06 evollu

@evollu But why do you think I always receive undefined, no matter if I open the app from the notification when it was completely closed or in background?

cmedinasoriano1 avatar Jun 11 '17 15:06 cmedinasoriano1

@cmedinasoriano you should see it in initNotif when you open the app from the notification when it was completely closed what os are you using

evollu avatar Jun 11 '17 15:06 evollu

@evollu react-native-fcm 7.0.0 RN 0.44.0 React 16.0.0-alpha.6 iOS 10.2

cmedinasoriano1 avatar Jun 11 '17 15:06 cmedinasoriano1

looking into it

evollu avatar Jun 11 '17 17:06 evollu

@cmedinasoriano should be fixed in 7.0.2. It is a bug caused by v7 refactoring

evollu avatar Jun 11 '17 17:06 evollu

Awesome! Thanks!

cmedinasoriano1 avatar Jun 11 '17 17:06 cmedinasoriano1

@cmedinasoriano LMK if it is fixed. then I will close the issue

evollu avatar Jun 11 '17 17:06 evollu

@evollu Working perfect! Thanks!

cmedinasoriano1 avatar Jun 11 '17 19:06 cmedinasoriano1

@evollu Hey! Seems to be not working for me.. :( I'm on 7.0.3 and if app is closed and click the push notification banner..

notif is "undefined" in: FCM.getInitialNotification().then((notif) => ... Any idea what could be the matter? I did all the setup that was needed. (I'm testing iOS..)

Twinski avatar Jun 19 '17 11:06 Twinski

@evollu I am also seeing this.

react-native 0.44 react-native-fcm 7.0.3 android version 6

From a fresh install of the app, when I first open the app, I see this in my logs:

getInitialNotification: { opened_from_tray: 1, fcm: { action: null } }

And every subsequent opening of the app (from fully closed) I see this:

getInitialNotification: { opened_from_tray: 1, fcm: { action: 'android.intent.action.MAIN' }, profile: 0 }

I have not sent any notifications since installing the app, no notifications are in the tray, and I open it from the home screen icon.

beeglebug avatar Jun 23 '17 16:06 beeglebug

@beeglebug this is expected. The initial notification is who triggers the open. For android it is android.intent.action.MAIN is you open by clicking the app icon. I expect people to look for particular custom field (like type) in notification and ignore the rest.

evollu avatar Jun 26 '17 13:06 evollu

@Twinski can you try the example project? you can schedule a local notification and test the banner

evollu avatar Jun 26 '17 13:06 evollu

@evollu I suppose that kind of makes sense although personally I would expect it to not trigger at all if the interaction didn't originate from a notification, but maybe that's just me

beeglebug avatar Jun 26 '17 14:06 beeglebug

I was expecting getInitialNotification to only be called if the app is opened from a notification as well. It's strange to me that it also is called when opened through the launcher.

AugustKarlstedt avatar Jun 28 '17 03:06 AugustKarlstedt

plus one

jqn avatar Nov 30 '17 21:11 jqn