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 :)
+1
- When app is in background, the notification goes in
FCM.oncallback.getInitialNotificationwon't have the value. To be clearInitialmeans launching the app, not opening the app. So I think the notification should trigger on callback. Check if it is the case. - 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.
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
@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
Depends on OS. It should return undefined or empty object.
@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?
@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 react-native-fcm 7.0.0 RN 0.44.0 React 16.0.0-alpha.6 iOS 10.2
looking into it
@cmedinasoriano should be fixed in 7.0.2. It is a bug caused by v7 refactoring
Awesome! Thanks!
@cmedinasoriano LMK if it is fixed. then I will close the issue
@evollu Working perfect! Thanks!
@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..)
@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 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.
@Twinski can you try the example project? you can schedule a local notification and test the banner
@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
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.
plus one