I use fcm-package (https://github.com/evollu/react-native-fcm) to send Firebase-Cloud-Messages to android.
Messages arrive at the phone.
If the app is in Background, the notification appears in the Statusbar. If I tap at the message, the App opens on main-screen - which is News_Screen in my case.
BUT I don't found a way to catch data of the notifications, after the App come to foreground.
FCM.on(FCMEvent.Notification, (notif) => {
don't get any event.
If the App is in foreground, and I send a Notification, the console.log
-output work.
Do I miss something special to be able to get event-data if app comes from background to foreground?
This is the structure of my app:
index.js ⇒ /app/index.js ⇒ (imports a StackNavigator)
Default Screen of StackNavigator is "News_Screen".
In News-Screen I have this:
async componentDidMount() {
// START Firebase Cloud Messaging
try {
let result = await FCM.requestPermissions({
badge: false,
sound: true,
alert: true
});
} catch (e) {
console.error(e);
}
FCM.getFCMToken().then(token => {
this.setState({token: token || ""});
console.log("First the token: ", token)
// store fcm token in your server
});
if (Platform.OS === "ios") {
FCM.getAPNSToken().then(token => {
console.log("APNS TOKEN (getFCMToken)", token);
});
}
FCM.on(FCMEvent.Notification, (notif) => {
if(notif.opened_from_tray){
setTimeout(()=>{
console.log("notif.opened_from_tray", notif);
}, 1000)
}
// there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
console.log("Message erhalten", notif);
if (notif.screen !== undefined) {
console.log("Jo, der Screen is", notif.screen, this.props.navigation);
this.props.navigation.navigate('Menu_Screen');
}
});
FCM.on(FCMEvent.RefreshToken, (token) => {
console.log("Again the token:", token)
// fcm token may not be available on first load, catch it here
});
}
The only console.log I got if I push the notification and app comes from background to foreground is the First the token...
Message.
How can I catch the Data of the notification. (E.g. if the Notification has as additional Object-Parmete: screen: 'Menu_Screen
, to switch to this screen after App get to foreground)
change your listener to something like this..
if the app is foreground, this line NotificationType.WillPresent will catch the notification data.